java - 在 typeahead 中选择具有更改 ID 的元素 java-selenium

标签 java selenium xpath

我有一个预输入下拉菜单。列表中的元素根据我输入的内容进行填充。填充的所有元素的 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/

相关文章:

java - 我如何检测我的方法在等待什么?

java - 如何将 PhoneGap 集成到 Ant 构建中?

python - Chromedriver only supports characters in the BMP error while sending Emoji with ChromeDriver Chrome using Selenium Python to Tkinter's label() 文本框

php - xPath:为什么以下不起作用?

python - Selenium 不随机点击(无错误代码)

xml - 我应该使用哪个:preceding::or preceding-sibling::?

java - 集合排序在android中的使用

java - 二维数组错误检查,战舰游戏

java - 在 Selenium 中获取图像 src

java - 使用 Selenium JAVA 的 IE 和 Chrome 的整页截图