【问题标题】:Select particular element from dropdown Selenium webdriver从下拉菜单中选择特定元素 Selenium webdriver
【发布时间】:2019-02-25 14:56:48
【问题描述】:

我有一个 web 元素,点击后会生成一个下拉列表,我可以点击 web 元素并查看使用代码的所有元素。

By mySelector = By.xpath("//li[contains(@class,'ant-select-dropdown-menu-item')]");
List<WebElement> myElements = driver.findElements(mySelector);
        for (WebElement e : myElements) {
            System.out.println(e.getAttribute("outerHTML"));
        }

还有

driver.findElement(By.xpath("//li[text() = '"
                    + user.getHomeLocation() + "']")).isDisplayed();

返回真

但我无法点击所需的值。我想点击威斯敏斯特

<li unselectable="unselectable" class="ant-select-dropdown-menu-item-active ant-select-dropdown-menu-item" role="menuitem" aria-selected="false" style="-moz-user-select: none;">Select</li>
<li unselectable="unselectable" class="ant-select-dropdown-menu-item" role="menuitem" aria-selected="false" style="-moz-user-select: none;">Basking Ridge</li>
<li unselectable="unselectable" class="ant-select-dropdown-menu-item" role="menuitem" aria-selected="false" style="-moz-user-select: none;">Highlands Ranch</li>
<li unselectable="unselectable" class="ant-select-dropdown-menu-item" role="menuitem" aria-selected="false" style="-moz-user-select: none;">Westminster</li>

【问题讨论】:

  • 您的代码试用版to click on web-element?此外,不是outerHTML 的输出,而是使用下拉列表的相关 HTML 更新问题

标签: selenium automation automated-tests


【解决方案1】:

尝试向带有所需项目的 xpath 定位器添加一个“包含”:

//li[contains(@class,'ant-select-dropdown-menu-item')][contains(.,'Westminster')]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    相关资源
    最近更新 更多