selenium - Webdriver 单击列表中的第二个元素

标签 selenium

我正在测试的页面有两个同名的元素,我需要单击第二个元素。 我可以使用以下方法获取元素:

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/

相关文章:

python-3.x - 如何单击 "Next"按钮直到它不再存在 - Python、Selenium、Requests

ant - 如何从 Ant 将参数传递给 Junit 测试?

javascript - 使用cucumber JS时出现错误 "HookUndefined Step"是什么意思?

Python/Selenium 组合多个列表并通过可见文本显示

python - selenium.common.exceptions.WebDriverException : Message: 'Geckodriver' executable may have wrong permissions using GeckoDriver Firefox Selenium Python

java - 如何在 Selenium Webdriver 中使用 "Upload Image Button"上传文件/照片

selenium - 来自Testng Selenium 的Gradle测试

python - Selenium ElementNotVisibleException : Message: element not interactable

java - 是否可以使用不同的 jenkins 作业运行每个testing.xml 文件?

javascript - 有没有办法在 Selenium 版本 4 和更高版本中以 headless 模式执行 UI 测试?