Click Here To Download Code
To Handle dropDownlist in selenium webdriver, We have to use "SelectElement" class in c#.Net.
It is located in package of "OpenQA.Selenium.Support.UI".
There are different representation of "Select" class in C#.Net and Java.
In C#:
Ex:
SelectElement se = new SelectElement( element ) ;
In Java:
Ex:
Select se = new select( element ) ;
We can Select element/option from DropDown List by 3-ways:
To demonstrate how to handle drop down list. I am going to consider "Facebook" application. Because it has three drop down list 1-Month 2-Day & 3-Year(Birthdate).
In Below example I want to select Jun 21 1984 as a Birthdate,
Example:
using System;
using NUnit.Framework;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace SleniumWebDriverWithCSharpTutorial
{
[TestFixture]
public class HandlingDropDownList
{
IWebDriver driver = null ;
IWebElement element = null ;
[SetUp]
public void SetUP()
{
driver = new FirefoxDriver();
driver.Manage().Window.Maximize();
driver.Url = "http://facebook.com";
}
[Test]
public void TestDropDownList()
{
//wait for to get Facebook Home page title
WebDriverWait wait = new WebDriverWait(driver,TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("month")));
//Handling Drop Down List to select Birth Month as "Jun".
SelectElement se = new SelectElement(driver.FindElement(By.Id("month")));
foreach (IWebElement item in se.Options)
{
if (item.Text == "Jun")
{
item.Click();
break ;
}
}
//Set previous element reference to b=null
se = null ;
//select By using Index Position
se = new SelectElement(driver.FindElement(By.Id("day")));
se.SelectByIndex(21);
//Set previous element reference to b=null
se = null ;
//Select By Using Value
se = new SelectElement(driver.FindElement(By.Id("year")));
se.SelectByValue("1984");
}
[TearDown]
public void TearDown()
{
if (element != null)
element = null ;
driver.Close();
}
}
}
Click Here To Download Code
To Handle dropDownlist in selenium webdriver, We have to use "SelectElement" class in c#.Net.
It is located in package of "OpenQA.Selenium.Support.UI".
There are different representation of "Select" class in C#.Net and Java.
In C#:
Ex:
SelectElement se = new SelectElement( element ) ;
In Java:
Ex:
Select se = new select( element ) ;
We can Select element/option from DropDown List by 3-ways:
- SelectByIndex( int ) - Select an option by index position.
- SelectByValue( String ) - Select an option by the value.
- SelectByText( String ) - Select an option by displayed Text.
To demonstrate how to handle drop down list. I am going to consider "Facebook" application. Because it has three drop down list 1-Month 2-Day & 3-Year(Birthdate).
In Below example I want to select Jun 21 1984 as a Birthdate,
Example:
using System;
using NUnit.Framework;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace SleniumWebDriverWithCSharpTutorial
{
[TestFixture]
public class HandlingDropDownList
{
IWebDriver driver = null ;
IWebElement element = null ;
[SetUp]
public void SetUP()
{
driver = new FirefoxDriver();
driver.Manage().Window.Maximize();
driver.Url = "http://facebook.com";
}
[Test]
public void TestDropDownList()
{
//wait for to get Facebook Home page title
WebDriverWait wait = new WebDriverWait(driver,TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("month")));
//Handling Drop Down List to select Birth Month as "Jun".
SelectElement se = new SelectElement(driver.FindElement(By.Id("month")));
foreach (IWebElement item in se.Options)
{
if (item.Text == "Jun")
{
item.Click();
break ;
}
}
//Set previous element reference to b=null
se = null ;
//select By using Index Position
se = new SelectElement(driver.FindElement(By.Id("day")));
se.SelectByIndex(21);
//Set previous element reference to b=null
se = null ;
//Select By Using Value
se = new SelectElement(driver.FindElement(By.Id("year")));
se.SelectByValue("1984");
}
[TearDown]
public void TearDown()
{
if (element != null)
element = null ;
driver.Close();
}
}
}
Click Here To Download Code