- 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 get selected option using Selenium WebDriver with Java?
We can get a selected option in a dropdown in Selenium webdriver. The method getFirstSelectedOption() returns the selected option in the dropdown. Once the option is fetched we can apply getText() method to fetch the text.
Let us consider the below dropdown Continents get its selected item−
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.support.ui.Select public class SelecedItem{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String u =" https://www.tutorialspoint.com/selenium/selenium_automation_practice.htm"driver.get(u); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // identify element WebElement t=driver.findElement(By.xpath("//*[@name='continents']")); //Select class for dropdown Select select = new Select(t); // select an item with text visible select.selectByVisibleText("Australia"); // get selected option with getFirstSelectedOption() with its text WebElement o = select.getFirstSelectedOption(); String selectedoption = o.getText(); System.out.println("Selected element: " + selectedoption); driver.close(); } }
Output
- Related Articles
- How to get selected option using Selenium WebDriver with Python?
- Get page title with Selenium WebDriver using Java.
- How to scroll down using Selenium WebDriver with Java?
- How do I get a parent HTML Tag with Selenium WebDriver using Java?
- How to type in textbox using Selenium WebDriver with Java?
- How to handle authentication popup with Selenium WebDriver using Java?
- Switch tabs using Selenium WebDriver with Java.
- How to get HTTP Response Code using Selenium WebDriver?
- How to scroll a specific DIV using Selenium WebDriver with Java?
- How to get Response Status Code with Selenium WebDriver?
- How to select checkboxes using selenium java webdriver?
- Capturing browser logs with Selenium WebDriver using Java.
- How to deal with ModalDialog using selenium webdriver?
- How to handle frame in Selenium WebDriver using java?
- How to use clickandwait in Selenium Webdriver using Java?

Advertisements