Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select an option from drop down using Selenium WebDriver C#?

I was trying for my web test selecting an option. An example can be found here: http://www.tizag.com/phpT/examples/formex.php

Everything works great except the selecting an option part. How to selecting an option by value or by label?

My Code:

using OpenQA.Selenium.Firefox; using OpenQA.Selenium; using System.Collections.ObjectModel; using System.Text.RegularExpressions; using System.Threading; using System.Diagnostics; using System.Runtime.InteropServices;  class GoogleSuggest {     static void Main()     {         IWebDriver driver = new FirefoxDriver();          //Notice navigation is slightly different than the Java version         //This is because 'get' is a keyword in C#         driver.Navigate().GoToUrl("http://www.tizag.com/phpT/examples/formex.php");         IWebElement query = driver.FindElement(By.Name("Fname"));         query.SendKeys("John");         driver.FindElement(By.Name("Lname")).SendKeys("Doe");         driver.FindElement(By.XPath("//input[@name='gender' and @value='Male']")).Click();         driver.FindElement(By.XPath("//input[@name='food[]' and @value='Chicken']")).Click();         driver.FindElement(By.Name("quote")).Clear();         driver.FindElement(By.Name("quote")).SendKeys("Be Present!");         driver.FindElement(By.Name("education")).SendKeys(Keys.Down + Keys.Enter); // working but that's not what i was looking for         // driver.FindElement(By.XPath("//option[@value='HighSchool']")).Click(); not working         //  driver.FindElement(By.XPath("/html/body/table[2]/tbody/tr/td[2]/table/tbody/tr/td/div[5]/form/select/option[2]")).Click(); not working         // driver.FindElement(By.XPath("id('examp')/x:form/x:select[1]/x:option[2]")).Click(); not working          } } 
like image 754
mirza Avatar asked Mar 11 '11 20:03

mirza


People also ask

How do I select a drop down?

The <select> element is used to create a drop-down list. The <select> element is most often used in a form, to collect user input. The name attribute is needed to reference the form data after the form is submitted (if you omit the name attribute, no data from the drop-down list will be submitted).

How do I select a dropdown value in automation?

A dropdown is represented by <select> tag and the options are represented by <option> tag. To select an option with its value we have to use the selectByValue method and pass the value attribute of the option that we want to select as a parameter to that method.


2 Answers

You must create a select element object from the drop down list.

 using OpenQA.Selenium.Support.UI;   // select the drop down list  var education = driver.FindElement(By.Name("education"));  //create select element object   var selectElement = new SelectElement(education);   //select by value  selectElement.SelectByValue("Jr.High");   // select by text  selectElement.SelectByText("HighSchool"); 

More info here

like image 62
Matthew Kelly Avatar answered Sep 19 '22 17:09

Matthew Kelly


Adding a point to this- I came across a problem that OpenQA.Selenium.Support.UI namespace was not available after installing Selenium.NET binding into the C# project. Later found out that we can easily install latest version of Selenium WebDriver Support Classes by running the command:

Install-Package Selenium.Support 

in NuGet Package Manager Console, or install Selenium.Support from NuGet Manager.

like image 43
Ravishankar S Avatar answered Sep 22 '22 17:09

Ravishankar S