0% found this document useful (0 votes)
11 views5 pages

Select 2 1

Uploaded by

abhinav8022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Select 2 1

Uploaded by

abhinav8022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

How to Select Dropdown in

Selenium
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:
1. Import the
package org.openqa.selenium.support.ui.Selec
t
2. Instantiate the drop-down box as an object, Select
in Selenium WebDriver
As an example, go to Mercury Tours’ Registration
page (https://demo.guru99.com/test/newtours/
register.php) and notice the “Country” drop-down box
there.

Step 1) Import the “Select” package.


import org.openqa.selenium.support.ui.Select;
Step 2) Declare the drop-down element as an
instance of the Select class.
In the example below, we named this instance as
“drpCountry”.
Select drpCountry = new
Select(driver.findElement(By.name("country")));
Step 3) Start Controlling it.
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.”
drpCountry.selectByVisibleText("ANTARCTICA");

Select Class in Selenium


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
speci es the web element location.
Select Methods in Selenium
The following are the most common methods used on
Selenium dropdown list.
#1) selectByVisibleText() and
deselectByVisibleText()
• Selects/deselects the option that displays the text
matching the parameter.
• Parameter: The exactly displayed text of a
particular option
Example:
fi
drpCountry.selectByVisibleText("ANTARCTICA");

#2) selectByValue() and deselectByValue()


• Selects/deselects the option whose “value”
attribute matches the speci ed parameter.
• Remember that not all drop-down options have
the same text and “value”, like in the example
below.
• Parameter: value of the “value” attribute
Example:

drpCountry.selectByValue("234");

#3) selectByIndex() and deselectByIndex()


• Selects/deselects the option at the given index.
• Parameter: the index of the option to be selected.
Example:
drpCountry.selectByIndex(0);

#4) isMultiple()
• Returns TRUE if the drop-down element allows
multiple selections at a time; FALSE if otherwise.
• Parameter: Not needed
Example
if (drpCountry.isMultiple())
{
//do something here
}

#5) deselectAll()
fi
• Clears all selected entries. This is only valid when
the drop-down element supports multiple
selections.
• Parameter: Not needed
Example:
drpCountry.deselectAll();

Selecting Items in a Multiple


SELECT elements
We can also use the selectByVisibleText() method in
selecting multiple options in a multi SELECT element.
As an example, we will take https://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 rst two options using
the selectByVisibleText() method.
fi
Command Description
selectByVisibleTe
selects/deselects an option by its
xt()/
displayed text
deselectByVisible
selectByValue()/
selects/deselects an option by
the value of its “value” attribute
deselectByValue()
selectByIndex()/
selects/deselects an option by its
index
deselectByIndex()
returns TRUE if the drop-down
isMultiple()
element allows multiple selection
at a time; FALSE if otherwise
deselectAll() deselects all previously selected
options

You might also like