Selenium WebDriver and DropDown Boxes

前端 未结 10 1733
星月不相逢
星月不相逢 2020-11-29 00:16

If I want to select an option of a dropdown box, there are several ways to do that. I always used:

driver.findElem         


        
10条回答
  •  自闭症患者
    2020-11-29 00:59

    Try the Select helper class and see if that makes any difference?

    String valueToSelect= "Germany";
    WebElement select = driver.findElement(By.id("selection"));
    Select dropDown = new Select(select);           
    String selected = dropDown.getFirstSelectedOption().getText();
    if(selected.equals(valueToSelect)) {//do stuff already selected}
    List Options = dropDown.getOptions();
    for(WebElement option:Options){
      if(option.getText().equals(valueToSelect)){
           option.click();  
      }
    }
    

提交回复
热议问题