9

I am having tough time in selecting value from drop down using C# binding of WebDriver. I have worked on neither C# nor WebDriver in past. I am using WebDriver - Selenium-dotnet2.0b3 with Visual Studio C# 2010 Express edition. I have added WebDriver.Common, WebDriver.Firefox and WebDriver.Remote to my solution. I tried using this -

IWebElement dateOfBirth = webdriver.FindElement(By.Id("join_birth_day"));
List<IWebElement> dateOfBirthOptions = (List<IWebElement>)dateOfBirth.FindElement(By.TagName("option"));

foreach(IWebElement dateOfBirthOption in dateOfBirthOptions)  
{
    if (dateOfBirthOption.Equals("3"))
    {
        dateOfBirthOption.Select();
    }
}

But got to see error, when running my solution in NUnit

LiveCams.CreateAccount.createAccount:
System.InvalidCastException : Unable to cast object of type 'OpenQA.Selenium.Firefox.FirefoxWebElement' to type 'System.Collections.Generic.List`1[OpenQA.Selenium.IWebElement]'.

And if I don't cast then would not be able to even build the solution. I guess I am missing some thing trivial here. Any one who could guide me here? Drop down selection used to be so simple in Selenium 1.0 :-/

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Tarun
  • 3,456
  • 10
  • 48
  • 82

4 Answers4

10

To select an Option from Drop Down use the below code

  1. To select a value based on Text

    new SelectElement(driver.FindElement(By.XPath(""))).SelectByText("");
    
  2. To select a value based on Value

    new SelectElement(driver.FindElement(By.XPath(""))).SelectByValue("");
    
  3. To select a value based on Index

    new SelectElement(driver.FindElement(By.XPath(""))).SelectByIndex(0);
    
Liam
  • 27,717
  • 28
  • 128
  • 190
Madhu
  • 479
  • 4
  • 10
  • 1
    Not to be nit-picky here, but for those that may be confused, when Selecting by Index, you do not need quotes in SelectByIndex(), just an int. – Rinktacular Jul 08 '15 at 16:31
  • This is now all covered in [documentation](http://stackoverflow.com/documentation/selenium-webdriver/6426/select-clas) – Liam Sep 22 '16 at 12:50
7

1) Using a SelectElement as already commented - How to select an option from drop down using Selenium WebDriver C#? The SelectElement belongs to the OpenQA.Selenium.Support.UI namespace.

2) You could also do something like this with css selectors:

WebElement dateOfBirth =  webdriver.FindElement(By.Id("join_birth_day"))
                              .FindElement(By.CssSelector("option[value='3']")).Select();
Community
  • 1
  • 1
Matthew Kelly
  • 3,094
  • 1
  • 19
  • 10
4

Use the Following Class SelectElement defined in OpenQA.Selenium.Support.UI namespace the word Select is already used in C# that is why its implementation is changed and class is named differently.

    // Summary:
    //     Initializes a new instance of the SelectElement class.
    //
    // Parameters: element - The element to be wrapped
    //
    public SelectElement(IWebElement element);

Create an object of this class and there is option of selection based on index, text and value.

    // Summary:
    //     Select the option by the index, as determined by the "index" attribute of
    //     the element.
    //
    // Parameters:
    //   index:
    //     The value of the index attribute of the option to be selected.
    public void SelectByIndex(int index);

    // Summary:
    //     Select all options by the text displayed.
    //
    // Parameters:
    //   text:
    //     The text of the option to be selected. If an exact match is not found, this
    //     method will perform a substring match.
    // Remarks:
    //     When given "Bar" this method would select an option like:
    //     <option value="foo">Bar</option>
    public void SelectByText(string text);

    // Summary:
    //     Select an option by the value.
    //
    // Parameters:
    //   value:
    //     The value of the option to be selected.
    // Remarks:
    //     When given "foo" this method will select an option like:
    //     <option value="foo">Bar</option>
    public void SelectByValue(string value);
vCillusion
  • 1,749
  • 20
  • 33
0
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://YourDDListpageURL.html");
            IWebElement element = driver.FindElement(By.XPath("//Select"));
            //You can locate the element by using the ID / Name as well IList
            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();
        }
    }
}
Dan Getz
  • 8,774
  • 6
  • 30
  • 64
Kotes
  • 11