Selenium WebDriver and DropDown Boxes

前端 未结 10 1670
星月不相逢
星月不相逢 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:46

    For some strange reason the SelectElement for webdriver (version 2.25.1.0) does not properly work with the firefoxdriver (Firefox 15). Sometimes it may not select an option from a dropdownlist. It does, however, seem to work with the chromedriver... This is a link to the chromedriver... just drop it in the bin dir.

    0 讨论(0)
  • 2020-11-29 00:46

    Just wrap your WebElement into Select Object as shown below

    Select dropdown = new Select(driver.findElement(By.id("identifier")));
    

    Once this is done you can select the required value in 3 ways. Consider an HTML file like this

    <html>
    <body>
    <select id = "designation">
    <option value = "MD">MD</option>
    <option value = "prog"> Programmer </option>
    <option value = "CEO"> CEO </option>
    </option>
    </select>
    <body>
    </html>
    

    Now to identify dropdown do

    Select dropdown = new Select(driver.findElement(By.id("designation")));
    

    To select its option say 'Programmer' you can do

    dropdown.selectByVisibleText("Programmer ");
    

    or

     dropdown.selectByIndex(1);
    

    or

     dropdown.selectByValue("prog");
    

    Happy Coding :)

    0 讨论(0)
  • 2020-11-29 00:53
    select = driver.FindElement(By.CssSelector("select[uniq id']"));
                    selectElement = new SelectElement(select);
                    var optionList =
                        driver.FindElements(By.CssSelector("select[uniq id']>option"));
                    selectElement.SelectByText(optionList[GenerateRandomNumber(1, optionList.Count())].Text);
    
    0 讨论(0)
  • 2020-11-29 00:54

    Try the following:

    import org.openqa.selenium.support.ui.Select;
    
    Select droplist = new Select(driver.findElement(By.Id("selection")));   
    droplist.selectByVisibleText("Germany");
    
    0 讨论(0)
  • 2020-11-29 00:54

    I have to struggle to find how to achieve especial those who are new to this tool (like me)

    C# code:

    IWebElement ddl = ffDriver.FindElement(By.Id("ddlGoTo")); 
    OpenQA.Selenium.Support.UI.SelectElement clickthis = new OpenQA.Selenium.Support.UI.SelectElement(ddl);
    clickthis.SelectByText("Your Text");
    

    hope this help others!

    0 讨论(0)
  • 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<WebElement> Options = dropDown.getOptions();
    for(WebElement option:Options){
      if(option.getText().equals(valueToSelect)){
           option.click();  
      }
    }
    
    0 讨论(0)
提交回复
热议问题