如何访问硒中具有相同类名的第二个元素 [英] How to access the second element that has the same class name in selenium

查看:25
本文介绍了如何访问硒中具有相同类名的第二个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页上有 2 个元素具有相同的类名,我正在尝试访问第二个元素,但无法访问.我尝试了 [position=1] 并将 [1] 放在我的 xpath 表达式的末尾

I have a 2 elements on my web page that has the same class name and I am trying to access the second element and I am unable to do that. I tried [position=1] and by putting [1] at the end of my xpath expression

driver.find_element_by_xpath("//div[@class='tableType value']")

上面返回了以下2个元素

the above returns the following 2 elements

我试过了

driver.find_element_by_xpath("//div[@class='tableType value']")[1]
driver.find_element_by_xpath("//div[@class='tableType value'][position=1]")

有人可以帮我解决这个问题吗?

Can someone please help me with this?

谢谢

推荐答案

使用

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

XPath starts counting at 1, so the second element is at position() 2

这篇关于如何访问硒中具有相同类名的第二个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆