Вы находитесь на странице: 1из 7

12/13/2018 How to Select Value from DropDown using Selenium Webdriver

(https://www.guru99.com/)

Home (/) Testing

SAP Web Must Learn! Big Data

Live Projects AI Blog (/blog/)

How to Select Value from DropDown using Selenium


Webdriver
In this tutorial, we will learn how to handle Drop Down and Multiple Select Operations.

Select Op on from Drop-Down Box


Before we can control drop-down boxes, we must do following two things:

1. Import the package org.openqa.selenium.support.ui.Select


2. Instantiate the drop-down box as a "Select" object in WebDriver

As an example, go to Mercury Tours' Registration page


(http://demo.guru99.com/test/newtours/register.php
(http://demo.guru99.com/test/newtours/register.php)) and notice the "Country" drop-down
box there.

(/images/image011(3).png)

Step 1

Import the "Select" package.

(/images/image012(1).png)

Step 2

Declare the drop-down element as an instance of the Select class. In the example below, we
named this instance as "drpCountry".
https://www.guru99.com/select-option-dropdown-selenium-webdriver.html 1/7
12/13/2018 How to Select Value from DropDown using Selenium Webdriver

(/images/image013(3).png)

Step 3

We can now start controlling "drpCountry" by using any of the available Select methods. The
sample code below will select the option "ANTARCTICA."

(/images/image014(3).png)

Selec ng Items in a Mul ple SELECT elements


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
(http://jsbin.com/osebed/2) as the base URL. It contains a drop-down box that allows
multiple selections at a time.

(/images/image015(3).png)

The code below will select the first two options using the selectByVisibleText() method.

(/images/image016(3).png)

https://www.guru99.com/select-option-dropdown-selenium-webdriver.html 2/7
12/13/2018 How to Select Value from DropDown using Selenium Webdriver
Select Methods
The following are the most common methods used on drop down list.

Method Description

selectByVisibleText() and
deselectByVisibleText() Example: Selects/deselects the option that displays the
text matching the parameter.
(/images/image017(3).png) Parameter: The exactly displayed text of a
particular option

selectByValue() and
deselectByValue() Example: Selects/deselects the option whose "value"
attribute matches the specified parameter.
(/images/image018(3).png) Parameter: value of the "value" attribute
Remember that not all drop-down options have
the same text and "value", like in the example
below.

(/images/image019(2).png)

selectByIndex() and
deselectByIndex() Example: Selects/deselects the option at the given index.
Parameter: the index of the option to be
(/images/image020(2).png) selected.

isMultiple() Example:
Returns TRUE if the drop-down element allows
multiple selections at a time; FALSE if
(/images/image021(2).png) otherwise.
No parameters needed

deselectAll() Example:
Clears all selected entries. This is only valid
(/images/image022(2).png) when the drop-down element supports multiple
selections.
No parameters needed

Here is the complete code

https://www.guru99.com/select-option-dropdown-selenium-webdriver.html 3/7
12/13/2018 How to Select Value from DropDown using Selenium Webdriver
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.firefox.marionette","C:\\geckodriver.exe");
String baseURL = "http://demo.guru99.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);
}
}

Summary
Element Command Description

Drop-Down selectByVisibleText()/ selects/deselects an option by its displayed text


Box deselectByVisibleText()

selectByValue()/ selects/deselects an option by the value of its


deselectByValue() "value" attribute

selectByIndex()/ selects/deselects an option by its index


deselectByIndex()

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.

 Prev (/accessing-forms-in-webdriver.html) Report a Bug

https://www.guru99.com/select-option-dropdown-selenium-webdriver.html 4/7
12/13/2018
 (/accessing-links-tables-selenium-webdriver.html)
How to Select Value from DropDown using Selenium Webdriver
Next

YOU MIGHT LIKE:

SELENIUM SELENIUM SELENIUM

(/selenium- (/using-robot-api- (/selenium-github.html)


alternatives.html) selenium.html) (/selenium-
(/selenium- (/using-robot-api- github.html)
alternatives.html) selenium.html)
Upload Selenium Script to
Top 15 Selenium Robot Class in Selenium GitHub
Alterna ves in 2018 Webdriver (/selenium-github.html)
(/selenium-alternatives.html) (/using-robot-api-
selenium.html)

SELENIUM SELENIUM SELENIUM

(/xslt-report- (/xpath-selenium.html) (/testng-execute-multiple-


selenium.html) (/xpath- test-suites.html)
(/xslt-report- selenium.html) (/testng-execute-
selenium.html) multiple-test-
XPath in Selenium
XSLT Report in Selenium WebDriver: Complete suites.html)
(/xslt-report-selenium.html) Tutorial TestNG: How to Run
(/xpath-selenium.html) Mul ple Test Suites in
Selenium
(/testng-execute-multiple-
test-suites.html)

Selenium Tutorials

1) Introduction (/introduction-to-selenium.html)

2) Intro WebDriver (/introduction-webdriver-comparison-selenium-rc.html)

3) Install Webdriver (/installing-selenium-webdriver.html)

4) First WebDriver Script (/first-webdriver-script.html)

5) Locators (/locators-in-selenium-ide.html)

6) Find Element Selenium (/find-element-selenium.html)

7) Forms & Webdriver (/accessing-forms-in-webdriver.html)

8) CheckBox & Radio Button WebDriver (/checkbox-and-radio-button-webdriver.html)

9) Click Image Webdriver (/click-on-image-in-selenium.html)


https://www.guru99.com/select-option-dropdown-selenium-webdriver.html 5/7
10) Selenium
12/13/2018 Webdriver DropDown (/select-option-dropdown-selenium-webdriver.html)
How to Select Value from DropDown using Selenium Webdriver

11) Links & Tables (/accessing-links-tables-selenium-webdriver.html)

12) Keyboard Mouse Events (/keyboard-mouse-events-files-webdriver.html)

13) Upload & Download File (/upload-download-file-selenium-webdriver.html)

14) XPath in Selenium (/xpath-selenium.html)

15) Alert & Popup handling (/alert-popup-handling-selenium.html)

16) Handle Web Table (/selenium-webtable.html)

17) Handling Dynamic Web Tables (/handling-dynamic-selenium-webdriver.html)

18) Desired Capabilities in Selenium (/desired-capabilities-selenium.html)

19) Verify Tooltip WebDriver (/verify-tooltip-selenium-webdriver.html)

20) Find Broken links (/find-broken-links-selenium-webdriver.html)

21) Gecko (Marionette) Driver (/gecko-marionette-driver-selenium.html)

22) Install TestNG in Eclipse (/install-testng-in-eclipse.html)

23) Selenium & TestNG (/all-about-testng-and-selenium.html)

24) Introduction to TestNG Groups (/introduction-testng-groups.html)

25) Test Case Priority in TestNG (/test-case-priority-testng.html)

2) Install IDE & FireBug (/install-selenuim-ide.html)

3) Introduction IDE (/introduction-selenuim-ide.html)

4) First Script (/first-selenium-test-script.html)

6) Enhancements (/enhancing-selenium-ide-script.html)

7) Variables, Echo, Alert, PopUp (/store-variables-handling-selenium-ide.html)

 (https://www.facebook.com/guru99com/) 
(https://twitter.com/guru99com) 
(https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)

(https://forms.aweber.com/form/46/724807646.htm)

About
About US (/about-us.html)
Advertise with Us (/advertise-us.html)
https://www.guru99.com/select-option-dropdown-selenium-webdriver.html 6/7
12/13/2018 Write For Us (/become-an-instructor.html)
How to Select Value from DropDown using Selenium Webdriver

Contact US (/contact-us.html)

Career Sugges on
SAP Career Suggestion Tool (/best-sap-module.html)
Software Testing as a Career (/software-testing-career-
complete-guide.html)
Certificates (/certificate-it-professional.html)

Interes ng
Books to Read! (/books.html)
Suggest a Tutorial
Blog (/blog/)
Quiz (/tests.html)
Review (/best-ergonomic-mouse.html)

Execute online
Execute Java Online (/try-java-editor.html)
Execute Javascript (/execute-javascript-online.html)
Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)

© Copyright - Guru99 2018


Privacy Policy (/privacy-policy.html)

https://www.guru99.com/select-option-dropdown-selenium-webdriver.html 7/7