我正在测试的页面有两个同名的元素,我需要单击第二个元素。 我可以使用以下方法获取元素:
driver.findElements(By.linkText("Services"));
但我不知道如何点击
第二个元素。
最佳答案
有两种方法可以做到这一点:
1)使用xpath,尝试以下方式。
driver.findElement(By.xpath("('xpath of the link')[2]"));//If you had given html, I could have added exact xpath.
2)使用 findElements() 您可以尝试以下操作:
List<WebElement> li = driver.findElements(By.linkText("Services"));;
li.get(1).click();//If there are only two such element, here 1 is index of 2nd element in list returned.
希望你能明白。 :)
关于selenium - Webdriver 单击列表中的第二个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21552928/