我想从一个 li(它看起来像一个下拉列表,但实际上不是)中选择第二个值(在我的示例中,Bellevue
)。这是一个屏幕截图:
我正在使用页面对象,我正在使用@FindBy annonations 来管理元素。这是我尝试过的:
@FindBy(how=How.CSS,using = "a[id^='ui-id-'][1]")
我收到错误:
The given selector a[id*='ui-id-'](1) is either invalid or does not result in a WebElement
或使用
@FindBy(how=How.CSS,using = "a[id^='ui-id-']"[1])`
我得到错误:
the type of expression must be an array but it resolved to string`.
如果我使用它就有效:
WebElement value = driver.findElements(By.cssSelector("a[id^='ui-id']")).get(3);
值.点击();
但我必须使用 @FindBy
并且我无法让它在 get(3) 方法上工作。
最佳答案
您不能在 CSS 选择器中使用 []
索引元素。
以下是您可能想尝试的方法:
@FindBy(how=How.CSS,using = "li.ui-menu-item:nth-of-type(2) > a[id^='ui-id-']")
或者使用 XPath
@FindBy(how=How.XPATH,using = "(//a[starts-with(@id, 'ui-id-')])[2]")
关于java - 如何使用 Selenium webdriver 找到具有相同 CSS 类的第二个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23384099/