jessica
jessica

Reputation: 1517

List of items in Selenium WebDriver using C#

How to write the same piece of code in C# ?

List<WebElement> displayedOptions = driver.FindElements(By.TagName("select"));
for (WebElement option : displayedOptions) 
 {
    if (option.displayed) 
    {
        displayedOptions.Add(option);
    }
 }

Upvotes: 7

Views: 39760

Answers (3)

Dmitri
Dmitri

Reputation: 1

If your SVG data looks like this :

<div id="content-data" class="col-md-12" style="height:666px;">
  <svg id="pie-draw-488172" width="492" height="616"><g transform="translate(226,318)">
    <text id="pie-text" cursor="default" y="-226" x="241">Compute (227,311)</text> 
    <text id="pie-text" cursor="default" y="-211" x="241">Database (98,005)</text>
    <text id="pie-text" cursor="default" y="-196" x="241">Storage&amp;Content Delivery (69,436)</text>
    <text id="pie-text" cursor="default" y="-181" x="241">Networking (30,874)</text>
    <text id="pie-text" cursor="default" y="-166" x="241">Other (11,273)</text>
  </svg>
</div>

then use this

public List<IWebElement> content_data = new List<IWebElement>();  
content_data = driver.FindElement(By.Id("content-data")).FindElements(By.TagName("text")).ToList();

Upvotes: 0

gtzinos
gtzinos

Reputation: 1197

FindElements returns ReadOnlyCollection. So its better to define

ReadOnlyCollection<IWebElement> displayedOptions  = driver.FindElements(By.TagName("select"));

for (WebElement option : displayedOptions) 
{
  if (option.displayed) 
  {
    //displayedOptions.Add(option); //You can't do that
    // do something else
  }
}

Upvotes: 1

Arran
Arran

Reputation: 25066

There is a class created specifically for select HTML elements (i.e. dropdowns).

It is the SelectElement class inside the OpenQA.Selenium.Support.UI namespace.

This is a wrapper around select elements, giving easy access to common things people use/interact with in select elements.

Your example would be translated into (using C# 3 or above, since I'm using LINQ):

IList<IWebElement> selectElements = driver.FindElements(By.TagName("select"));
var displayedSelectElements = selectElements.Where(se => se.Displayed);

It's important to know what this code does. It will first find all select elements and put them into a new list.

It will then filter those out, to only the select elements that are displayed, that is, their .Displayed property is true. This is a direct translation of your example code.

However, you've not really specified what you are trying to do, and I think the example is better suited as this:

var selectElement = new SelectElement(driver.FindElement(By.Id("something")));
var displayedOptions = selectElement.Options.Where(o => o.Displayed);

The above would find a specific select elements, and filter the options within that select to only those that are displayed. Again, that is, have their .Displayed property as true.

Edit

Since the above code is what you need, but you want in the form of a for loop, a similar thing would look like:

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)
    {
        // do something
        // like add to a new list?
    }
}

Upvotes: 10

Related Questions