当尝试自动化我们的应用程序时,有两个同名的按钮。
我无法找到识别这些的方法。请让我知道在 java 中识别 selenium webdriver 中这些元素的方法是什么
最佳答案
使用 xpath 或 css(首选)找到所需的按钮,如下所示。
让我们考虑下面的示例 html。
<html><head></head><body>
<button class="same_class">follow</button>
<button class="same_class">follow</button>
</body></html>
现在您可以将 css 与 nth-of-type 一起使用,如下所示
button.same_class:nth-of-type(2)
CSS 屏幕截图:
您也可以使用 xpath 实现相同的目的。
(//button[@class='same_class'])[2]
XPath 屏幕截图:
根据需要更改括号中的索引(索引以 1
开头而不是 0)
关于java - 如何访问具有相同类名的第二个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57085416/