【问题标题】:select element in typeahead with changing ID java- selenium在 typeahead 中选择具有更改 ID java-selenium 的元素
【发布时间】:2019-10-06 11:10:15
【问题描述】:

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

【问题讨论】:

  • 感谢开发者的回复。我刚刚找到答案,我将在下面发布。

标签: java selenium xpath


【解决方案1】:

我自己刚刚找到了这个问题的答案。我的代码中有错误。

  driver.findElements(By.xpath("//a[starts-with(@id, 'typeahead')]"))

应该是这样的

driver.findElements(By.xpath("//*[starts-with(@id, 'typeahead')]"))

我不知道到底发生了什么,但显然它解决了问题,现在我的列表中包含了预先输入下拉列表的所有元素。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-21
    • 2013-04-13
    • 2014-12-09
    • 2012-11-12
    • 1970-01-01
    • 2012-03-07
    • 1970-01-01
    • 2011-11-29
    相关资源
    最近更新 更多