【问题标题】:Selenium Duplicate Elements marked with ==$0标有 ==$0 的硒重复元素
【发布时间】:2019-10-16 19:08:48
【问题描述】:
如何处理 selenium 中的重复元素,其中重复元素用==$0 标记?
转到 www.google.com 并搜索 google 搜索按钮,我已经尝试过迭代器和创建列表,但是,这是处理 ==$0 的正确方法
driver.findElements(By.xpath("//input[@aria-label='Google Search']"));
我想选择第二个元素。
【问题讨论】:
标签:
java
selenium
google-chrome
google-chrome-devtools
code-inspection
【解决方案1】:
您想要单击的可见按钮是具有“FPdoLc VlcLAe”类的 div 的后代,因此您可以使用
选择它
driver.findElements(By.xpath("//div[@class='FPdoLc VlcLAe']//input[@name='btnK']"));
不可见的是具有 class= "VlcLAe" 但没有 "FPdoLc" 的 div 的后代,所以这就是区别。
【解决方案2】:
您在使用 //input[@aria-label='Google Search']
时获得了两个值
一种可能的解决方案是使用:(//input[@aria-label='Google Search'])[2](实心括号中的值,即 2 应该没有空格)
例如 - driver.findElement(By.xpath("(//input[@aria-label='Google Search'])[ 2 ]"); //(实心括号中的值,即2应该没有空格)