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

后端 未结 11 803
闹比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
    

提交回复
热议问题