A dropdown (also called a select box) is a user interface element that allows a user to choose one (or sometimes multiple) options from a predefined list. In HTML, dropdowns are typically represented by the <select> element containing <option> tags, or they can be custom-built using <div>, <ul>, or JavaScript frameworks.
Dropdowns in Playwright Context
When automating with Playwright, dropdown handling means interacting with these elements to select values, verify selected options, or test behavior when selections change.
Types of Dropdowns in Playwright
Standard HTML Dropdowns (Native Select Menus):
- Built using <select> and <option>.
- Easy to interact with because they follow HTML standards.
- Playwright recognizes them directly as form elements.
- Created using <div>, <li>, or JavaScript-based UI libraries
- Options might be hidden in the DOM until the dropdown is clicked.
- Require more interaction (like clicking to expand, then choosing an item).
Why Dropdown Handling is Important
- Dropdowns are commonly used for forms, filters, country/state selectors, and settings menus.
- Testing dropdowns ensures that the correct options appear, can be selected, and that the application reacts appropriately to user choices.
- It also validates dynamic behavior, such as when choosing one option updates another dropdown (e.g., country → state selection).
- Handling a dropdown in Playwright Java can be done in different ways depending on the type of dropdown:
- Dynamic loading: Options may not be present in the DOM until the dropdown is clicked.
- Hidden elements: Some frameworks hide the real <select> and use a styled <div> or <span>.
- Multiple selections: Some dropdowns allow multiple options, requiring special handling.
- Asynchronous behavior: Selecting an option may trigger AJAX requests or page updates, so synchronization (waiting) is crucial.
1. HTML <select>
Tag Dropdown
If the dropdown is built using the standard <select>
tag, you can use the selectOption()
method.
2. Custom Dropdown (Div-based or React/Angular dropdowns)
For custom dropdowns, you typically need to click the dropdown to expand it, then click the desired option.
1. Handling HTML <select>
Dropdown
<select id="dropdown">
<option value="option1">Java</option>
<option value="option2">Python</option>
<option value="option3">JavaScript</option>
</select>
import com.microsoft.playwright.*;
public class DropdownSelectExample {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
Page page = browser.newPage();
// Go to a page with a select dropdown
page.navigate("https://rahulshettyacademy.com/AutomationPractice/");
// Select option by value
page.locator("#dropdown-class-example").selectOption("option2");
// To see the execution
Thread.sleep(2000);
System.out.println("Option selected.");
}
}
}
import com.microsoft.playwright.*;
public class CustomDropdownExample {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
Page page = browser.newPage();
page.navigate("https://seleniumpractise.blogspot.com/2016/08/bootstrap-dropdown-example-for-selenium.html#");
// Scroll to view if not visible
page.locator("#menu1").scrollIntoViewIfNeeded();
// Click to open the custom dropdown
page.locator("#menu1").click();
// Click the desired option
page.locator("ul[class='dropdown-menu']>li:nth-child(1)>a").click();
System.out.println("Custom option selected.");
}
}
}
Suggested Posts:
1. Trace Viewer in Playwright
2. File Download in Playwright
3. Playwright with JUnit
4. Handle Dynamic Webtable in Playwright
5. Page Object Model in Playwright