从下拉菜单中选择特定元素Selenium Webdriver

时间:2018-09-21 08:28:38

标签: selenium automation automated-tests

我有一个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>

1 个答案:

答案 0 :(得分:0)

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

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