![](/img/trans.png)
[英]How to access the second element that has the same class name with Python
[英]How to access the second element that has the same class name in selenium
我的網頁上有 2 個元素具有相同的類名,我正在嘗試訪問第二個元素,但我無法做到這一點。 我嘗試了 [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
嗨,請找到以下代碼以單擊具有相同類名的第二個元素。 [1] 表示如果您想點擊第二個,[2] 表示您想點擊第三個....
driver.find_elements_by_class_name('classname')[1].click()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.