- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Gecko driver, Selecting value from a dropdown list using Selenium
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(); } }
Output
- Related Articles
- What is the command used to register gecko driver in Selenium?
- How to Select Value from DropDown using Selenium Webdriver?
- How to select an item from a dropdown list using Selenium WebDriver with java?
- How to select a value from a static dropdown in Selenium?
- Using Selenium Web Driver to retrieve value of a HTML input.
- How to de-register a driver from driver manager’s drivers list using JDBC?
- How to use the gecko executable with Selenium?
- Get text using selenium web driver in python?
- How to Auto-Populate Other Cells When Selecting Values in an Excel Dropdown List?
- What are the different ways to select an option from a dropdown using Selenium Webdriver?
- How to create a dropdown list using JavaScript?
- Handling DropDown And Multiple Select in Webdriver using Selenium
- What is Selenium Internet Explorer Driver or IE Driver?
- How will you select a particular value in a dropdown without using the methods of Select class in Selenium?
- What is selenium web driver?
