我的网页上有两个具有相同类名的元素,我正在尝试访问第二个元素,但我无法做到这一点。我尝试 [position=1] 并将 [1] 放在我的 xpath 表达式的末尾
driver.find_element_by_xpath("//div[@class='tableType value']")
以上返回以下2个元素
我试过了
driver.find_element_by_xpath("//div[@class='tableType value']")[1]
driver.find_element_by_xpath("//div[@class='tableType value'][position=1]")
有人可以帮我解决这个问题吗?
谢谢
最佳答案
使用
driver.find_element_by_xpath("(//div[@class='tableType value'])[2]")
或
driver.find_element_by_xpath("(//div[@class='tableType value'])[position()=2]")
XPath 从 1 开始计数,所以第二个元素在 position()
2
关于python - 如何访问selenium中具有相同类名的第二个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23819397/