2017-12-05 1 views
0

Ich versuche ein Element aus einer Autosuggestion-Liste mithilfe von Xpath zu finden, bekomme aber InvalidSelectorException. Unten ist mein Code: -Das Element kann nicht mit Hilfe von XPath aus der Dropdown-Liste "Automatisch vorschlagen" gefunden werden.

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(); 

Bitte schlagen Sie auch vor Wie kann ich es mit anderen Methoden finden, wenn möglich.

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 --> 
+0

Ihre XPath ok aussieht. Überprüfen Sie nur, ob der Selektor mehrere Elemente zurückgibt –

+0

Entschuldigung, aber ich bin neu dazu Wie soll ich das überprüfen? –

+0

https://www.freeformatter.com/xpat-tester.html. Sie können Ihre XPath-Gültigkeit hier überprüfen. –

Antwort

0

Ich weiß nicht, über Ihre Locators, da ich kein Element mit id 'Engagement Code' sehen. Wenn Sie mehr HTML abdecken, wird es leichter zu beantworten sein. Ich würde vorschlagen, Firefox-Plugin Firebug zu verwenden, um Elemente und Firepath zu überprüfen, um Ihren xpath zu überprüfen. Ich habe Google Seite Autosuggestion Box ausprobiert. Geben Sie das Code-Snippet unten ein. Sehen Sie, ob Ihnen das weiterhilft.

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; 
     } 
    } 
+0

Danke Mann, der für mich arbeitete. Es war eine große Hilfe. –

0

Um click(select) die autosuggestion111, können Sie den folgenden Codeblock verwenden:

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(); 
Verwandte Themen