Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select each option in a drop down using Selenium WebDriver C#

Tags:

c#

selenium

I'm not able to select options in a drop down list. I think I need to have .Select or SelectElement, but there is no such option.

Sample code:

IWebDriver ffbrowser = new FirefoxDriver();
ffbrowser.Navigate().GoToUrl("http://www.amazon.com/");
ffbrowser.Manage().Window.Maximize();

Thread.Sleep(500);

IWebElement ddl = ffbrowser.FindElement(By.Name("url"));
int numofitems = ddl.FindElements(By.TagName("option")).Count;

for (int i = 1; i < numofitems; i++)
{
    ffbrowser.select("TagName = option", "index = i");
}

The "select" in "ffbrowser.select" is reported as an error:

Error 1 'OpenQA.Selenium.IWebDriver' does not contain a definition for 'select' and no extension method 'select' accepting a first argument of type 'OpenQA.Selenium.IWebDriver' could be found (are you missing a using directive or an assembly reference?)

My project references include Selenium.WebDriverBackedSelenium, Thoughtworks.Selenium.Core, WebDriver, WebDriver.Support

and I have

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
like image 662
Ben Walker Avatar asked Mar 20 '13 21:03

Ben Walker


People also ask

How do you select an option from the Select drop down in Selenium?

Initially you have to import the Select class and afterward you have to make the case of Select class. After making the case of Select class, you can perform select strategies on that occasion to choose the choices from dropdown list. Step-by-step approach: Import webdriver from selenium module.

How do you select multiple items in a drop down list in Selenium?

To handle drop down and multi select list using Selenium WebDriver, we need to use Select class. The Select class is a Webdriver class which provides the implementation of the HTML SELECT tag. It exposes several “Select By” and “Deselect By” type methods.

How a drop down value is selected using WebDriver?

The 'Select' class in Selenium WebDriver is used for selecting and deselecting option in a dropdown. The objects of Select type can be initialized by passing the dropdown webElement as parameter to its constructor.


1 Answers

Depending what version of Selenium WebDriver you are using you can use the SelectElement class, which will be included in OpenQA.Selenium.Support.UI.
For example:

SelectElement selector = new SelectElement(element);
selector.SelectByIndex(1);

Where the element is your drop down box.

like image 127
Nashibukasan Avatar answered Oct 05 '22 18:10

Nashibukasan