This article will show how we can handle dropdowns without a select tag using playwright java. Earlier, we discussed the Playwright selectOption method to handle Playwright's basic <select> dropdown.
The selectOption method throws an error if the target element is not an <select>. Let's discuss how to handle dynamic dropdowns or select an option when the select option is not available.
Let's select the country Aruba from the bootstrap country dropdown here.
page.navigate("http://autopract.com/selenium/dropdown4/");
page.locator("//span[@class='caret']").click();
Locator countries = page.locator("//div[@role='combobox']");
List<String> allInnerTexts = countries.allInnerTexts();
- We can see here it got all the texts from the locator.
- Now we can iterate it through advanced for loop.
for (String innerTexts : allInnerTexts) {
if (innerTexts.contains("Aruba")) {
countries.click();
}
}
- Doing this will select the specified country with the help of a simple if statement.
- This is how we can select from the dynamic dropdown using playwright java.
- Log in to post comments