无法使用自动建议下拉列表中的xpath定位元素

时间:2017-12-05 09:38:55

标签: selenium xpath

我正在尝试使用xpath从自动提示列表中找到一个元素但是获取InvalidSelectorException。以下是我的代码: -

WebDriverWait wait=new WebDriverWait(driver, 10);
driver.findElement(By.xpath(".//*[@id='EngagementCode']")).sendKeys("111");
   wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//[@id='typeahead-5-5946-option-0']/a"))).click();  

另外请建议如果可能的话,如何使用不同的方法找到它。

Please find HTML

 <ul id="typeahead-5-5946" class="dropdown-menu ng-isolate-scope" ng-show="isOpen() && !moveInProgress" ng-style="{top: position().top+'px', left: position().left+'px'}" style="display: block; top: 34px; left: 15px;" role="listbox" aria-hidden="false" typeahead-popup="" matches="matches" active="activeIdx" select="select(activeIdx)" move-in-progress="moveInProgress" query="query" position="position">
<!-- ngRepeat: match in matches track by $index -->
<li id="typeahead-5-5946-option-0" class="ng-scope active" ng-repeat="match in matches track by $index" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index)" role="option">
<a class="ng-binding ng-scope" href="" tabindex="-1" ng-bind-html="match.label | typeaheadHighlight:query">
xxx
<strong>111</strong>
x  - xxx services_xxx
</a>
</li>
<!-- end ngRepeat: match in matches track by $index -->

2 个答案:

答案 0 :(得分:0)

我不知道你的定位器,因为我看不到任何带有id'参与代码'的元素。我可以提供更多的HTML覆盖,然后它将更容易回答。 我建议使用Firefox插件Firebug来检查元素和Firepath来验证你的xpath。 我试过Google页面自动提示框。给下面的代码片段,看看这是否对您有帮助。

 WebElement searchBox= driver.findElement(By.xpath("//input[@id='lst-ib']"));
    System.out.println("searchBox.isDisplayed(): "+searchBox.isDisplayed());
    searchBox.sendKeys("Seleni");

    System.out.println("search value: "+searchBox.getAttribute("value"));
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='sbqs_c']")));

    List<WebElement> autoTextList = driver.findElements(By.xpath("//div[@class='sbqs_c']"));

    System.out.println("List size: "+autoTextList.size());
    for(WebElement listItem : autoTextList){
        System.out.println("List item: "+listItem.getText());
        if(listItem.getText().equals("selenium webdriver")) {
            listItem.click();
            break;
        }
    }

答案 1 :(得分:0)

click(select) autosuggestion 111 ,您可以使用以下代码块:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement elem = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[@class='ng-scope active']/a[@class='ng-binding ng-scope']/strong")));
elem.click();
相关问题