Python/Selenium在xpath中使用条件

2024-06-26 00:25:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要使用以下xpath在一些结构基本相同的网页中查找特定元素:

//*[@id="detailPCTtableHeader"]/tbody/tr[10]/td[2]/div/span/span[1]/text()

问题是在某些页面tr[10]tr[11]。你知道吗

有没有办法告诉Selenium搜索tr[10]tr[11]?你知道吗


Tags: textdivid元素网页页面结构xpath
2条回答

您可以使用position()来评估索引:

element = driver.find_element_by_xpath("id('detailPCTtableHeader')/tbody/tr[position()=10 or position()=11]/td[2]/div/span/span[1]")

"Is there a way to tell Selenium to search for tr[10] or tr[11]?"

使用position()

tr[position() = 10 or position() = 11]

或者,如果一次只存在tr[10]tr[11]中的一个,这意味着它总是最后一个tr,您可以简单地使用last()

tr[last()]

相关问题 更多 >