There is a class created specifically for select HTML elements (for example, drop-down menus).
This is the SelectElement class inside the OpenQA.Selenium.Support.UI namespace.
This is a wrapper around select elements that provides easy access to common things that people use / interact with select elements.
Your example will translate to (using C # 3 or higher since I use LINQ):
IList<IWebElement> selectElements = driver.FindElements(By.TagName("select")); var displayedSelectElements = selectElements.Where(se => se.Displayed);
It is important to know what this code does. First, it will find all select elements and put them in a new list.
Then it will filter them, only the displayed select elements, i.e. their .Displayed true property. This is a direct translation of the sample code.
However, you didn’t actually indicate what you are trying to do, and I think the example is better for this:
var selectElement = new SelectElement(driver.FindElement(By.Id("something"))); var displayedOptions = selectElement.Options.Where(o => o.Displayed);
The above can find specific select elements and filter the parameters within that select only for those that are displayed. Again, they have the .Displayed property as true .
Edit
Since the above code is what you need, but you want in the form of a for loop, something like this would look like this:
var selectElement = new SelectElement(driver.FindElement(By.Id("something"))); var allOptions = selectElement.Options; for (int i = 0; i < allOptions.Length; i++) { if (allOptions[i].Displayed) {
Arran
source share