
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
Different Ways to Select an Option from a Dropdown Using Selenium WebDriver
We have different ways to select an option from a dropdown using Selenium webdriver. This is done with the help of the Select class. A dropdown in the html code is represented by select tag.
The options in a dropdown are represented by the option tag. Also, we have to add the statement org.openqa.selenium.support.ui.Select to the code to work with dropdown.
The different methods to select an option from a dropdown are listed below −
selectByIndex – index of the option to be selected by the dropdown is passed as a parameter. The index starts from 0.
WebElement e = driver.findElement(By.className("opt")); Select s = new Select(e); s.selectByIndex(0);
selectByValue – value attribute of the option to be selected by the dropdown is passed as a parameter. The options in the dropdown should have the value attribute so that this method can be used.
WebElement e = driver.findElement(By.className("opt")); Select s = new Select(e); s.selectByIndex("option 1");
selectByVisibleText – visible text of the option to be selected by the dropdown is passed as a parameter.
WebElement e = driver.findElement(By.className("opt")); Select s = new Select(e); s.selectByVisibleText("Selenium");
Let us try to select an option from the below dropdown −
Example
Code Implementation with selectByIndex
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 DrpSelectByIndex{ 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("https://www.tutorialspoint.com/tutor_connect/index.php/"); //identify dropdown WebElement n = driver.findElement(By.name("selType")); //select option by index Select s = new Select(n); s.selectByIndex(0); driver.quit(); } }
Code Implementation with selectByValue
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 DrpSelectByValue{ 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("https://www.tutorialspoint.com/tutor_connect/index.php/"); //identify dropdown WebElement n = driver.findElement(By.name("selType")); //select option by value attribute of option Select s = new Select(n); s.selectByValue("subject"); driver.quit(); } }
Code Implementation with selectByVisibleText
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 DrpSelectByVisibleTxt{ 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("https://www.tutorialspoint.com/tutor_connect/index.php/"); //identify dropdown WebElement n = driver.findElement(By.name("selType")); //select option by value attribute of option Select s = new Select(n); s.selectByVisibleText("By Name"); driver.quit(); } }