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); } } 
+7
source share
3 answers

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

FindElements returns ReadOnlyCollection. Therefore, it is better to determine

 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 } } 
+1
source

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(); 
0
source

All Articles