Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

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;

1 Answer

0 votes
by (62.9k points)

Depending on 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.

Here is an example to better illustrate how to get all the items in a drop-down list and to select an item from the drop-down list.

A sample Html code for drop-down list

<select>

  <option>Milk</option>

  <option>Coffee</option>

  <option>Tea</option>

</select>

Code below gets all the items from the drop-down list above and selects the item 'Coffee'.The logic of the code is as follows

Step 1. Create an interface of the web element tag Step 2. Create an IList with all the child elements of web element tag Step 3. Select the Drop List item "Coffee"

using System;

using System.Collections.Generic;

using OpenQA.Selenium;

using OpenQA.Selenium.Firefox;

using OpenQA.Selenium.Support.UI;

namespace SeleniumTests

{

    class DropDownListSelection

    {

        static void Main(string[] args)

        {

            IWebDriver driver = new FirefoxDriver(); 

            driver.Navigate().GoToUrl("http://DropDownList.html");

            IWebElement element = driver.FindElement(By.XPath("//Select"));

            IList<IWebElement> AllDropDownList =    element.FindElements(By.XPath("//option"));

            int DpListCount = AllDropDownList.Count;

            for (int i = 0; i < DpListCount; i++)

            {

                if (AllDropDownList[i].Text == "Coffee")

                 {

                    AllDropDownList[i].Click();

                 }

            }

            Console.WriteLine(DpListCount);

            Console.ReadLine();

        }

    }

}

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium training

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...