Selenium

Handle Dynamic Dropdowns in Selenium Webdriver with Java

Handling dynamic dropdowns in Selenium WebDriver with Java is a crucial skill for web automation testers. Dropdown fields are commonly found in web applications and allow users to select from a list of predefined options. However, interacting with dynamic dropdowns can be challenging as the options may change based on user actions or external factors. In this guide, we will explore the different techniques and methods to effectively handle dynamic dropdowns using Selenium WebDriver with Java.

1. What Is a Dropdown Field?

A dropdown field, also known as a select field, is an HTML element that presents a list of options to the user. It allows the user to choose single or multiple options from the list. Dropdown fields are typically represented by the <select> tag in HTML, with each option enclosed within an <option> tag.

Fig. 1: Handle Dynamic Dropdowns.
Fig. 1: Handle Dynamic Dropdowns.

2. Introduction To Select Class in Selenium WebDriver

The Select class in Selenium WebDriver provides methods to interact with dropdown fields. It allows you to select options, deselect options, and retrieve selected options from dropdowns. To use the Select class, you need to import the org.openqa.selenium.support.ui.Select package.

Here’s an example of initializing a Select object for a dropdown field:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

WebDriver driver = new ChromeDriver();
WebElement dropdownElement = driver.findElement(By.id("myDropdown"));
Select dropdown = new Select(dropdownElement);

3. How To Select Values From Dropdown Fields?

To select a value from a dropdown field, you can use the selectByVisibleText(), selectByValue(), or selectByIndex() methods provided by the Select class. Here’s an example using selectByVisibleText():

dropdown.selectByVisibleText("Option 1");

This code selects the option with the visible text “Option 1” from the dropdown. You can replace "Option 1" with the desired option you want to select.

4. How To Select Values From a Single Dropdown List Field?

To select values from a single dropdown list field, you can use the techniques mentioned in Section 3. You can choose the desired option by visible text, value, or index. For example:

// Select by visible text
dropdown.selectByVisibleText("Option 2");

// Select by value
dropdown.selectByValue("value2");

// Select by index
dropdown.selectByIndex(2);

5. Checking All the Options in the Field

If you want to verify the available options in a dropdown field, you can retrieve all the options as a list of WebElements using the getOptions() method. Here’s an example:

List options = dropdown.getOptions();
for (WebElement option : options) {
    System.out.println(option.getText());
}

This code retrieves all the options from the dropdown and prints their visible text.

6. How To Select Multiple Values From a Multiselect Dropdown List?

In a multiselect dropdown list, you can select multiple values simultaneously. To select multiple options, you can use the selectByVisibleText(), selectByValue(), or selectByIndex() methods multiple times. Here’s an example:

Select multiSelectDropdown = new Select(driver.findElement(By.id("multiSelectDropdown")));

multiSelectDropdown.selectByVisibleText("Option 1");
multiSelectDropdown.selectByVisibleText

("Option 3");
multiSelectDropdown.selectByVisibleText("Option 5");

This code selects “Option 1,” “Option 3,” and “Option 5” from the multiselect dropdown.

7. How to Deselect Values From a Dropdown List Field?

To deselect options from a dropdown list field, you can use the deselectByVisibleText(), deselectByValue(), or deselectByIndex() methods provided by the Select class. These methods work for both single and multiselect dropdowns. Here’s an example:

// Deselect by visible text
dropdown.deselectByVisibleText("Option 1");

// Deselect by value
dropdown.deselectByValue("value2");

// Deselect by index
dropdown.deselectByIndex(2);

8. Deselecting the Options Using Visible Text

In a multiselect dropdown, you can also deselect options by their visible text using the deselectByVisibleText() method. Here’s an example:

Select multiSelectDropdown = new Select(driver.findElement(By.id("multiSelectDropdown")));

multiSelectDropdown.deselectByVisibleText("Option 3");
multiSelectDropdown.deselectByVisibleText("Option 5");

This code deselects “Option 3” and “Option 5” from the multiselect dropdown.

9. Checking the First Selected Option

To retrieve the first selected option from a dropdown field, you can use the getFirstSelectedOption() method. This method returns the selected option as a WebElement. Here’s an example:

WebElement selectedOption = dropdown.getFirstSelectedOption();
System.out.println(selectedOption.getText());

This code retrieves the first selected option from the dropdown and prints its visible text.

10. Test Execution: Handling Dynamic Dropdowns in Selenium WebDriver Java

When handling dynamic dropdowns, you may encounter scenarios where the options are loaded dynamically based on user interactions or external data sources. In such cases, you need to wait for the options to be available before interacting with the dropdown.

You can use explicit waits with conditions like ExpectedConditions.visibilityOfElementLocated() or ExpectedConditions.elementToBeClickable() to wait for the dropdown element to be visible or clickable. Once the dropdown is ready, you can proceed with selecting options using the techniques discussed earlier.

Here’s an example:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement dropdownElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("myDropdown")));
Select dropdown = new Select(dropdownElement);

dropdown.selectByVisibleText("Option 1");

In this code, we use an explicit wait to wait for the dropdown element to be visible, and then we proceed with selecting “Option 1” from the dropdown.

11. From the IDE Using TestNG

If you are using an IDE like IntelliJ IDEA or Eclipse, you can integrate TestNG framework for test execution and management. TestNG allows you to organize and execute your tests efficiently.

To handle dynamic dropdowns in TestNG, you can create test methods annotated with @Test and use the techniques mentioned earlier within those methods. TestNG provides features like assertions and test dependencies to enhance your test scripts.

Here’s an example using TestNG:

import org.testng.annotations.Test;

public class DropdownTest {
    @Test
    public void selectOptionFromDropdown() {
        // Test logic for handling dynamic dropdowns
    }
}

In this code, we define a test method selectOptionFromDropdown() annotated with @Test. You can place your test logic for handling dynamic dropdowns inside this method.

11. Conclusion

Knowing how to handle dynamic dropdowns in Selenium WebDriver with Java is essential for web automation testing. By using the Select class methods, you can select, deselect, and retrieve options from dropdown fields. Additionally, explicit waits can be employed to handle dynamic scenarios where options are loaded dynamically. Integrating TestNG with your IDE provides a robust framework for organizing and executing tests.

Remember to explore the Selenium WebDriver documentation and experiment with different scenarios to enhance your understanding and proficiency in handling dynamic dropdowns. With these techniques and practices, you’ll be well-equipped to handle dropdown fields effectively during your web automation testing endeavors.

Odysseas Mourtzoukos

Mourtzoukos Odysseas is studying to become a software engineer, at Harokopio University of Athens. Along with his studies, he is getting involved with different projects on gaming development and web applications. He is looking forward to sharing his knowledge and experience with the world.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button