Dropdown
Select elements for practicing dropdown interactions.
Native selects and custom dropdowns
Native select: selectOption()
For a real <select> element, do not click the options. Call selectOption() with the option's value, label or index:
const country = page.getByLabel('Select a country');
await country.selectOption('Poland'); // by value
await country.selectOption({ label: 'Germany' }); // by visible text
await expect(country).toHaveValue('Germany');Multiple selection
Pass an array to select several options at once. It replaces the previous selection. Assert with toHaveValues().
const languages = page.getByLabel('Languages you speak');
await languages.selectOption(['English', 'Polish']);
await expect(languages).toHaveValues(['English', 'Polish']);Custom dropdowns are not selects
Many sites build dropdowns from buttons and lists. selectOption() does not work on those. Interact like a user: open the list, then click the option by its role.
await page.getByRole('button', { name: 'Shirt size' }).click();
await page.getByRole('option', { name: 'Medium' }).click();
await expect(page.getByRole('button', { name: 'Shirt size' })).toContainText('Medium');To tell them apart, look at the element in DevTools. A <select> tag means selectOption(); anything else means clicking.
Country
Selected country: none
Languages
Selected languages: none
Shirt size
A custom dropdown built from a button and a list.
Shirt size
Selected size: none
Next in Basic Elements · 6 of 6Hover →Trigger hover states, hover-only menus and tooltips.
Related exercises
- FormsIntermediateAutocompleteDrive two comboboxes: one filtered in the browser, one that asks the server.
- Content & AttributesIntermediateARIA WidgetsLocate widgets by their ARIA state, and assert whole structures with aria snapshots.
- Basic ElementsBeginnerRadio ButtonsSelect a radio option and verify the group allows only one selection.
Read the guide: Playwright locators: a practical guide with exercises