Selenium
How to Download & Install Selenium WebDriver
Selenium installation is a 3 step process: Install Java SDK Install Eclipe Install Selenium...
In this tutorial, we will learn how to handle DropDown in Selenium and Multiple Select Operations.
The Select Class in Selenium is a method used to implement the HTML SELECT tag. The html select tag provides helper methods to select and deselect the elements. The Select class is an ordinary class so New keyword is used to create its object and it specifies the web element location.
Following is a step by step process on how to select value from dropdown in Selenium:
Before handling dropdown in Selenium and controlling drop-down boxes, we must do following two things:
As an example, go to Mercury Tours' Registration page (http://demo.gtupapers.com/test/newtours/register.php) and notice the "Country" drop-down box there.
Step 1
Import the "Select" package.
Step 2
Declare the drop-down element as an instance of the Select class. In the example below, we named this instance as "drpCountry".
Step 3
We can now start controlling "drpCountry" by using any of the available Select methods to select dropdown in Selenium. The sample code below will select the option "ANTARCTICA."
We can also use the selectByVisibleText() method in selecting multiple options in a multi SELECT element. As an example, we will take http://jsbin.com/osebed/2 as the base URL. It contains a drop-down box that allows multiple selections at a time.
The code below will select the first two options using the selectByVisibleText() method.
The following are the most common methods used on Selenium dropdown list.
package newpackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By;
public class accessDropDown {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
String baseURL = "http://demo.gtupapers.com/test/newtours/register.php";
WebDriver driver = new FirefoxDriver();
driver.get(baseURL);
Select drpCountry = new Select(driver.findElement(By.name("country")));
drpCountry.selectByVisibleText("ANTARCTICA");
//Selecting Items in a Multiple SELECT elements
driver.get("http://jsbin.com/osebed/2");
Select fruits = new Select(driver.findElement(By.id("fruits")));
fruits.selectByVisibleText("Banana");
fruits.selectByIndex(1);
}
}
| Element | Command | Description |
|---|---|---|
| Drop-Down Box | selectByVisibleText()/ deselectByVisibleText() | selects/deselects an option by its displayed text |
| selectByValue()/ deselectByValue() | selects/deselects an option by the value of its "value" attribute | |
| selectByIndex()/ deselectByIndex() | selects/deselects an option by its index | |
| isMultiple() | returns TRUE if the drop-down element allows multiple selection at a time; FALSE if otherwise | |
| deselectAll() | deselects all previously selected options |
To control drop-down boxes, you must first import the org.openqa.selenium.support.ui.Select package and then create a Select instance.
Selenium installation is a 3 step process: Install Java SDK Install Eclipe Install Selenium...
In Selenium automation, if the elements are not found by the general locators like id, class,...
File IO is a critical part of any software process. We frequently create a file, open it & update...
What is Jenkins? Jenkins is the leading open-source continuous integration tool developed by...
Intellij is an IDE that helps you to write better and faster code. Intellij can be used in the...
We will use the Mercury Tours website as our web application under test. It is an online flight...