
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Select Value from Dropdown List Using Selenium Gecko Driver
We can use the Selenium webdriver scripts in Firefox (versions>47) with the help of the geckodriver.exe executable file. First, we have to download this file from the following link −
https://github.com/mozilla/geckodriver/releases
Once we navigate to the mentioned URL, we have to click a link based on the operating system (Windows, Linux or Mac) we are presently using. As the download gets over, a zip file is created. We have to extract the zip file and store the geckodriver.exe file in a desired location.
Then, we have to configure the path of the geckodriver.exe file using the System.setProperty method along with creating an object of the FirefoxDriver class.
To select a value from a dropdown we have to use the Select class. A dropdown in html code is represented by <select> tag and the options in a dropdown is represented by <option> tag.
There are multiple methods available in Select class to select an option from dropdown −
selectByIndex – The index of the option to be selected by the dropdown is passed as parameter to this method. The index starts from 0.
WebElement e = driver.findElement(By.id("slc")); Select s = new Select(e); s.selectByIndex(0);
selectByValue – The value attribute of the option to be selected by the dropdown is passed as parameter to this method. The options in the dropdown should have the value attribute so that this method can be used.
WebElement e = driver.findElement(By.id("slc")); Select s = new Select(e); s.selectByValue("option 1");
selectByVisibleText – The visible text of the option to be selected by the dropdown is passed as parameter to this method.
WebElement e = driver.findElement(By.id("slc")); Select s = new Select(e); s.selectByVisibleText("Selenium");
Let us see an example of a dropdown along with its html code −
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.support.ui.Select public class DrpDwnValue{ public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //URL launch driver.get("http://www.uitestpractice.com/Students/Select"); //identify dropdown WebElement m = driver.findElement(By.id("countriesSingle")); //select option by index Select s = new Select(m); s.selectByIndex(0); //get option selected String st = s.getFirstSelectedOption().getText(); System.out.println("Option selected by Index: " + st); //select option by value s.selectByValue("england"); //get option selected String r = s.getFirstSelectedOption().getText(); System.out.println("Option selected by Value: " + r); //select option by visible text s.selectByVisibleText("United states of America"); //get option selected String l = s.getFirstSelectedOption().getText(); System.out.println("Option selected by Visible Text: " + l); driver.quit(); } }