2017-05-29 1 views
1

Ich muss ein Land aus Drop-Down von https://www.parcelhero.com wählen, aber wenn ich den folgenden Code dafür verwende, funktioniert es manchmal manchmal nicht und gibt Fehler von Element nicht gefunden (xpath ("// * [@ id = 'dvQuoteFrom']/div/button "))Wie benutzt man den Selen-Webdriver, um eine Option des Drop-down-Menüs Bootstrap auszuwählen?

driver.findElement(By.xpath("//*[@id='dvQuoteFrom']/div/button")).click(); 

     Thread.sleep(4000); 

     WebElement txt = driver.findElement(By.xpath("html/body/div[14]/div/div/input")); 
txt.sendKeys("Great Britain"); 
List <WebElement> InnerDropdown1 =driver.findElements(By.xpath("//*[@class='active']")); 
     for(WebElement option1 : InnerDropdown1) 
     { System.out.println(option1.getText()); 

     if(option1.getText().contains("Great Britain")) { 
      option1.click(); 
      break; 
     } 
     } 

Als ich verwenden WebElement txt = driver.findElement (By.className (" bs-searchbox ")); Dann habe ich auch den Fehler gefunden, einen Elementfehler zu finden.

Bitte helfen Sie mir, ein Land meiner Wahl aus dem Dropdown-Menü auszuwählen.

Antwort

0

Versuchen Sie, dies zu tun. Ich habe ein paar Ihrer Locators geändert und dies auf der von Ihnen erwähnten Seite getestet.

public void foo() { 
    driver.get("https://www.parcelhero.com/"); 

    //This will be your dropdown button. This needs to be clicked first. 
    driver.findElement(By.xpath("//button[contains(@data-id,'ConsignorAddressCountryId')]")).click(); 

    //These are your input boxes. I found 3 input boxes on the same page having the same identifiers. We dont want to rely on using index based xpath so here we'll get all instances of input boxes. 
    List<WebElement> elems = driver.findElements(By.xpath("//*[contains(@class,'bs-searchbox')]//input")); 
    for (WebElement elem : elems) { 

     //Here we check if the input box is visible. If I'm not mistaken, there will only be 1 visible input box at a time because you have to click on the dropdown button first. 
     if (elem.isDisplayed()) { 
      //If visible, just enter your country of choice 
      elem.sendKeys("American Samoa"); 
      //Assuming you always enter exact match string for your test, you can use the org.openqa.selenium.Keys for sendKeys() 
      elem.sendKeys(Keys.ENTER); 
      break; 
     } 
    } 
} 
+0

Danke, ich werde es überprüfen – user2044296

Verwandte Themen