How to get all options in a drop-down list by Selenium WebDriver using C#?

后端 未结 11 802
闹比i
闹比i 2020-12-30 02:30

I\'m new to both C# and Selenium WebDriver.

I know how to select/click on an option in a drop-down list, but I\'ve a problem before that. Since the drop-down list i

相关标签:
11条回答
  • 2020-12-30 02:55

    You can use selenium.Support to use the SelectElement class, this class have a property "Options" that is what you are looking for, I created an extension method to convert your web element to a select element

    public static SelectElement AsDropDown(this IWebElement webElement)
    {
        return new SelectElement(webElement);
    }
    

    then you could use it like this

    var elem = driver.FindElement(By.XPath("//select[@name='time_zone']"));
    var options = elem.AsDropDown().Options
    
    0 讨论(0)
  • You can try using the WebDriver.Support SelectElement found in OpenQA.Selenium.Support.UI.Selected namespace to access the option list of a select list:

    IWebElement elem = driver.FindElement(By.XPath("//select[@name='time_zone']"));
    
    SelectElement selectList = new SelectElement(elem);
    IList<IWebElement> options = selectList.Options;
    

    You can then access each option as an IWebElement, such as:

    IWebElement firstOption = options[0];
    Assert.AreEqual(firstOption.GetAttribute("value"), "-09:00");
    
    0 讨论(0)
  • 2020-12-30 02:58
    Select select = new Select(driver.findElement(By.id("searchDropdownBox")));
    
    select.getOptions();//will get all options as List<WebElement>
    
    0 讨论(0)
  • 2020-12-30 03:00
     WebElement element = driver.findElement(By.id("inst_state"));
            Select s = new Select(element);
            List <WebElement> elementcount = s.getOptions();
    
            System.out.println(elementcount.size());
            for(int i=0 ;i<elementcount.size();i++)
            {
                String value = elementcount.get(i).getText();
                System.out.println(value);
    
            }
    
    0 讨论(0)
  • 2020-12-30 03:02
    WebElement drop_down =driver.findElement(By.id("Category"));
    Select se = new Select(drop_down);
    for(int i=0 ;i<se.getOptions().size(); i++)
    System.out.println(se.getOptions().get(i).getAttribute("value"));
    
    0 讨论(0)
提交回复
热议问题