Skip to main content

Working with Dropdown in Selenium Webdriver

WebDriver has a class called “Select” to interact with dropdown. Dropdown is an another WebElement like textbox, radio button, links etc.

We use below methods frequently to interact with Dropdown using Select class.

selectByIndex(int index)
selectByValue(String value)
selectByVisibleText(String text)
getOptions()
getFirstSelectedOption()

Note: Select class can be found in org.openqa.selenium.support.ui package

Sample Dropdown:

selectByIndex(int index):
This method is used to select dropdown value by using index. This method accept integer as an argument. Index value always start from 0 (Zero). So if we want to select 2nd value from dropdown we have to give index 1.

Syntax:
Select s = new Select(dropdown_WebElement);
s.selectByIndex(1);

Example:

package selenium;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class HandleDropdown {

public static void main(String[] args) {
// Open firefox browser
WebDriver driver = new FirefoxDriver();
// Maximize the browser
driver.manage().window().maximize();
//implicit wait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Navigate to url
driver.get("https://www.irctc.co.in/eticketing/userSignUp.jsf");
// Find the dropdown
WebElement sQuestion= driver.findElement(By.id("userRegistrationForm:securityQ"));
// Create object for Select class
Select s = new Select(sQuestion);
// Select second dropdown value using index
s.selectByIndex(1);
}
}

selectByValue(String value):
This method is used to select dropdown value by using value. This method accepts String as an argument. All dropdowns may not have ‘value’ attribute in html code. If we have ‘value’ attribute for dropdown option, then we can use this method.

Syntax:
Select s = new Select(dropdown_WebElement);
s.selectByValue(“value”);


In above dropdown we have ‘value’ attribute. So here we can use selectByValue() method.

Example:

package selenium;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class HandleDropdown {

public static void main(String[] args) {
// Open firefox browser
WebDriver driver = new FirefoxDriver();
// Maximize the browser
driver.manage().window().maximize();
//implicit wait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Navigate to url
driver.get("https://www.irctc.co.in/eticketing/userSignUp.jsf");
// Find the dropdown
WebElement sQuestion= driver.findElement(By.id("userRegistrationForm:securityQ"));
// Create object for Select class
Select s = new Select(sQuestion);
// Select dropdown value using 'Value'
s.selectByValue("1");
}
}

selectByVisibleText(String text):
This method is used to select dropdown value by using visible text. This method accepts String as an argument. Visible text means the text which is visible in dropdown.
Select all options that display text matching the argument. That is, when given "Bar" this would select an option like: <option value="foo">Bar</option>

Note: This is case sensitive.

Syntax:
Select s = new Select(dropdown_WebElement);
s. selectByVisibleText (“visible text”);

Example:

package selenium;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class HandleDropdown {

public static void main(String[] args) {
// Open firefox browser
WebDriver driver = new FirefoxDriver();
// Maximize the browser
driver.manage().window().maximize();
//implicit wait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Navigate to url
driver.get("https://www.irctc.co.in/eticketing/userSignUp.jsf");
// Find the dropdown
WebElement sQuestion= driver.findElement(By.id("userRegistrationForm:securityQ"));
// Create object for Select class
Select s = new Select(sQuestion);
// Select dropdown value using 'visible text'
s.selectByVisibleText("Who was your Childhood hero?");
}
}

getOptions():
This method is used to get all options from dropdown. It return List of WebElement ( List<WebElement>) then we can iterate using for loop or iterator and using getText() method we can extract values.

Syntax:
Select s = new Select(dropdown_WebElement);
s. getOptions();

Example:

package selenium;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class HandleDropdown {

public static void main(String[] args) {
// Open firefox browser
WebDriver driver = new FirefoxDriver();
// Maximize the browser
driver.manage().window().maximize();
//implicit wait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Navigate to url
driver.get("https://www.irctc.co.in/eticketing/userSignUp.jsf");
// Find the dropdown
WebElement sQuestion= driver.findElement(By.id("userRegistrationForm:securityQ"));
// Create object for Select class
Select s = new Select(sQuestion);
// get all dropdown options using getOptions method
List<WebElement> allOptions = s.getOptions();
for (int i = 0; i < allOptions.size(); i++) {
System.out.println("Dropdown Option "+(i+1)+" is: "+allOptions.get(i).getText());
}
}
}

Output:
Dropdown Option 1 is: ----Select your Security Question----
Dropdown Option 2 is: What is your pet name?
Dropdown Option 3 is: What was the name of your first school?
Dropdown Option 4 is: Who was your Childhood hero?
Dropdown Option 5 is: What is your favorite past-time?
Dropdown Option 6 is: What is your all time favorite sports team?
Dropdown Option 7 is: What is your fathers middle name?
Dropdown Option 8 is: What make was your first car or bike?
Dropdown Option 9 is: Where did you first meet your spouse?

getFirstSelectedOption():
It will return the first selected option in dropdown. Using getText() method we can retrieve the value.
In some dropdowns as per client requirement some default value will be display. To check that default value is displaying or not we can use this method.

Syntax:
Select s = new Select(dropdown_WebElement);
s. getFirstSelectedOption ();

Example:

package selenium;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class HandleDropdown {

public static void main(String[] args) {
// Open firefox browser
WebDriver driver = new FirefoxDriver();
// Maximize the browser
driver.manage().window().maximize();
//implicit wait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Navigate to url
driver.get("https://www.irctc.co.in/eticketing/userSignUp.jsf");
// Find the dropdown
WebElement sQuestion= driver.findElement(By.id("userRegistrationForm:securityQ"));
// Create object for Select class
Select s = new Select(sQuestion);
// get default selected option
WebElement firstOption = s.getFirstSelectedOption();
System.out.println("First selected option is: "+firstOption.getText());
}
}

Output:
First selected option is: ----Select your Security Question----

Comments

Popular posts from this blog

What is the difference between Selenium and QTP?

Feature QTP(UFT) Selenium Language Support VB Script Java, C#, Ruby, Python, Perl, PHP Windows (Non-browser) based Application support Yes No Browser support Google Chrome (uptill ver 23) Internet Explorer , Firefox ( ver 21) Google Chrome , Internet Explorer , Firefox , Opera , HtmlUnit, Safari Environment Support Only Windows Windows , Linux , Solaris OS X , iOS, Android, Others (If brower & JVM or Javascript support exists) Mobile (Phones & Tablets) Support Different commercial product i.e. HP UFT Mobile (formerly known as MobileCloud for QTP) Android , iPhone & iPad , Blackberry , Headless WebKit Framework Easily integrated with HP Quality Center or HP ALM (separate commercial products) Selenium + Eclipse + Maven / ANT + Jenkins...

Operating Systems supported by Selenium Webdriver

Microsoft Windows: Most versions of MS Windows that are currently still supported by Microsoft should work with Selenium. Altough here's the list of OS's we currently run tests against before each release:     Windows XP (to be unsupported on April 8, 2014)     Windows 7     Windows 8     Windows 8.1 If your version of windows is not listed, it does not mean Selenium won't attempt to support it. That only means we don't continually run tests on that particular version of Windows. Apple OS X : We currently do not use any version of OS X in our automated tests against the selenium project. However most developers on the project are using a recent version of OS X and we'll continue to support the current stable release and often the previous release. Linux: We test mainly on Ubuntu, but other variations of Linux should also work where the browser manufacturers support them. iOS : The first lines of the ...