我有一个预输入下拉菜单。列表中的元素根据我输入的内容进行填充。填充的所有元素的 ID 类似于“typeahead-123”,其中“typeahead”保持不变,但“123”对于所有元素的 id 不断变化。现在我如何捕获 java 列表中下拉列表中填充的所有元素。
List<WebElement> options = driver.findElements(By.xpath("//a[starts-with(@id, 'typeahead')]"));
for (WebElement option1 : options) {
if (option1.getText().equals(mychoice))
option1.click();
return "Pass";
}
最佳答案
我自己刚刚找到了问题的答案。我的代码中有一个错误。
driver.findElements(By.xpath("//a[starts-with(@id, 'typeahead')]"))
这应该是
driver.findElements(By.xpath("//*[starts-with(@id, 'typeahead')]"))
我不知道到底发生了什么,但显然它解决了问题,现在我的列表中包含了预输入下拉列表的所有元素。谢谢。
关于java - 在 typeahead 中选择具有更改 ID 的元素 java-selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58256662/