Selenium 4 Beginner Tutorial 10 | How to handle DropDown
Automation Step by Step Automation Step by Step
511K subscribers
26,512 views
0

 Published On Dec 30, 2021

All FREE courses - https://automationstepbystep.com/
00:00 Introduction
00:36 How to select an option from drop-down
08:18 How to store dropdown options in a list
14:48 How to deselect an option from drop-down

How to select an option from Dropdown
Step 1 - Import select class from selenium
import org.openqa.selenium.support.ui.Select;

Step 2 - Create a select object by referring to the select element
WebElement selectElement = driver.findElement(By.id("option"));
Select selectObject = new Select(selectElement);

Step 3 - select object will give you multiple commands to select
selectObject.selectByIndex(1);
selectObject.selectByValue("option 2");
selectObject.selectByVisibleText("Option 3");

How to store dropdown options in a list
Step 1 - Import select class from selenium
import org.openqa.selenium.support.ui.Select;

Step 2 - Create a list of type WebElement for the dropdown
List<WebElement> allAvailableOptions = selectObject.getOptions();
OR
List<WebElement> options = driver.findElements(By.id("option"));

Step 3 - Create a LOOP to access all options
for(WebElement option : options) {
System.out.println(option.getText()); }

How to deselect an option from Dropdown
selectObject.deselectByIndex(1);
selectObject.deselectByValue("option 2");
selectObject.deselectByVisibleText("Option 3");


#seleniumbeginnertutorials
Stories by Raghav - https://automationstepbystep.com/stor...
My Udemy Courses - https://automationstepbystep.com/udem...

Every LIKE & SUBSCRIPTION gives me great motivation to keep working for you

You can support my mission for education by sharing this knowledge and helping as many people as you can

If my work has helped you, consider helping any animal near you, in any way you can

Never Stop Learning
Raghav

show more

Share/Embed