2017-05-29 7 views
1

Je dois sélectionner un pays de la liste déroulante de https://www.parcelhero.com mais quand j'utilise le code ci-dessous pour cela, parfois ça marche parfois pas, donnant l'erreur de Element non trouvé (xpath ("// * [@ id = 'dvQuoteFrom']/div/bouton "))Comment utiliser sélénium webdriver pour sélectionner une option du menu déroulant Bootstrap?

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

Quand je WebElement txt = driver.findElement (By.className (" bs-searchbox ")); alors aussi j'ai eu l'uable pour trouver l'erreur d'élément.

S'il vous plaît aidez-moi à sélectionner un pays de mon choix dans la liste déroulante pays?

Répondre

0

Essayez de le faire. J'ai changé quelques-uns de vos locators et testé cela sur la page que vous avez mentionnée.

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

Merci, je vais le vérifier – user2044296