- 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
How to Select Value from DropDown using Selenium Webdriver?
We can select an option from the dropdown with its value attribute in Selenium webdriver by using the Select class. . A dropdown is represented by <select> tag and the options are represented by <option> tag.
To select an option with its value we have to use the selectByValue method and pass the value attribute of the option that we want to select as a parameter to that method.
Syntax
WebElement v = driver.findElement(By.name("selt")); Select s = new Select(v); s.selectByValue("val1");
Let us see the html code of a dropdown having value attribute for its options.
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 SelectDrpDwn{ 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(4, TimeUnit.SECONDS); //URL launch driver.get("http://www.uitestpractice.com/Students/Select"); //identify dropdown WebElement n = driver.findElement(By.id("countriesSingle")); Select sl = new Select(n); //option by value sl.selectByValue("usa"); //get option with text String s = sl.getFirstSelectedOption().getText(); System.out.println("Value selected: : " + s); driver.quit(); } }
Output
- Related Articles
- How to select an item from a dropdown list using Selenium WebDriver with java?
- Handling DropDown And Multiple Select in Webdriver using Selenium
- What are the different ways to select an option from a dropdown using Selenium Webdriver?
- How to select a value from a static dropdown in Selenium?
- How to select checkboxes using selenium java webdriver?
- How to Select date from a datepicker with Selenium Webdriver using Python?
- Selenium WebDriver and DropDown Boxes.
- How to get all options of a dropdown using Python Selenium webdriver?
- How can I select date from a datepicker div using Selenium Webdriver?
- Gecko driver, Selecting value from a dropdown list using Selenium
- How to select value from a drop down using Selenium IDE?
- How will you select a particular value in a dropdown without using the methods of Select class in Selenium?
- How to select the Date Picker In Selenium WebDriver?
- How to select an option in a static dropdown in Selenium?
- How to Verify Tooltip using Selenium WebDriver?

Advertisements