selectOption() works on native <select> elements, or on a label that points to one. Call it on a React Select or Material UI combobox built from divs and Playwright throws. Custom dropdowns need a click to open and a click on the option by role or visible text. Afterward, assert the value the dropdown shows, because a click that did not throw proves nothing.
Key takeaways
- Use
selectOption()only when the target is a real<select>or a label pointing to one. A dropdown built from divs makes it throw, and there you click the trigger and then the option by role. - Open the element in DevTools before you write the test. A
<select>takesselectOption(), while a<div>or<button>trigger takes clicks. - A native
<select multiple>takes an array in a singleselectOption()call. A custom multi-select needs one click per option and anEscapepress to close the list. - To check that an option is disabled, call
toBeDisabled()on itsgetByRole('option', { name })locator. - After a selection, assert the value the dropdown displays, with
toHaveValue()on a native select ortoHaveText()on a custom combobox. A click that raised no error does not show that the right item was picked.
Native <select> elements
The simplest case is a standard HTML <select> element.
<label for="country">Country</label>
<select id="country">
<option value="">Select a country</option>
<option value="us">United States</option>
<option value="de">Germany</option>
<option value="pl">Poland</option>
</select>// Select by visible text
await page.getByLabel('Country').selectOption('Germany');
// Select by value attribute
await page.getByLabel('Country').selectOption({ value: 'de' });
// Select by index (0-based, avoid this — fragile)
await page.getByLabel('Country').selectOption({ index: 2 });
// Verify selection
await expect(page.getByLabel('Country')).toHaveValue('de');
// Select multiple (for <select multiple>)
await page.getByLabel('Tags').selectOption(['playwright', 'typescript']);Custom dropdown components
Many apps replace the native <select> with a component from a library such as React Select, Headless UI or Material UI. These render divs and buttons, so selectOption() does not apply.
Pattern 1, click to open and click the option
// Custom dropdown: click to open, click to select
await page.getByRole('combobox', { name: 'Country' }).click();
await page.getByRole('option', { name: 'Germany' }).click();
// Verify
await expect(page.getByRole('combobox', { name: 'Country' })).toHaveText('Germany');If the trigger is a button and the list has the listbox role:
await page.getByRole('button', { name: 'Country' }).click();
await page.getByRole('listbox').getByRole('option', { name: 'Germany' }).click();Pattern 2, type to filter and select
If the dropdown has a search input, fill it to filter the list:
// Type to filter
await page.getByRole('combobox', { name: 'Country' }).fill('Ger');
// Wait for filtered results to appear
await page.getByRole('option', { name: 'Germany' }).waitFor();
// Click the option
await page.getByRole('option', { name: 'Germany' }).click();Pattern 3, keyboard navigation
Some dropdowns open and move with the arrow keys:
await page.getByLabel('Country').focus();
await page.keyboard.press('ArrowDown'); // Open dropdown
await page.keyboard.press('ArrowDown'); // Move to first option
await page.keyboard.press('ArrowDown'); // Move to second option
await page.keyboard.press('Enter'); // SelectIn some dropdowns, typing a letter jumps to a matching option:
await page.getByLabel('Country').focus();
await page.keyboard.type('G'); // Jump to Germany
await page.keyboard.press('Enter');Inspecting the actual dropdown implementation
Before writing the test, inspect the element in DevTools:
- Right-click the dropdown → Inspect
- Check the element type, whether
<select>or<div>/<button>. - If custom, interact with it manually and watch the DOM changes
- Check
roleattribute or ARIA roles
React Select
The class names below exist only when the app sets classNamePrefix="react-select" on the component. Without that prop React Select adds only generated classes, which are no good as selectors:
// React Select combobox
const dropdown = page.locator('.react-select__control');
await dropdown.click();
// Type to filter
await dropdown.locator('input').fill('Ger');
await page.locator('.react-select__option', { hasText: 'Germany' }).click();
// Verify
await expect(page.locator('.react-select__single-value')).toHaveText('Germany');
// Clear selection
await page.locator('.react-select__clear-indicator').click();If the app sets no prefix, use getByRole. React Select v5 gives its input the combobox role and its options the option role:
// Better — uses semantic roles
await page.getByRole('combobox', { name: 'Country' }).click();
await page.getByRole('option', { name: 'Germany' }).click();Multi-select dropdowns
// Native <select multiple>
await page.getByLabel('Skills').selectOption(['playwright', 'typescript', 'cicd']);
// Custom multi-select: click multiple options
await page.getByRole('button', { name: 'Skills' }).click();
await page.getByRole('option', { name: 'Playwright' }).click();
await page.getByRole('option', { name: 'TypeScript' }).click();
await page.getByRole('option', { name: 'CI/CD' }).click();
// Close the dropdown
await page.keyboard.press('Escape');
// Verify selections
await expect(page.getByTestId('selected-skills')).toContainText('Playwright');
await expect(page.getByTestId('selected-skills')).toContainText('TypeScript');Date dropdowns (day, month, year selectors)
Date inputs with separate month/day/year dropdowns:
await page.getByLabel('Month').selectOption('June');
await page.getByLabel('Day').selectOption('15');
await page.getByLabel('Year').selectOption('1990');For <input type="date">:
// The value must be in yyyy-mm-dd format
await page.getByLabel('Date of birth').fill('1990-06-15');
// Verify
await expect(page.getByLabel('Date of birth')).toHaveValue('1990-06-15');Verifying dropdown options
// Verify all options are present
const options = await page.getByLabel('Country').locator('option').allTextContents();
expect(options).toContain('Germany');
expect(options).toContain('Poland');
// Verify a specific option is disabled
await expect(page.getByRole('option', { name: 'Out of stock item' })).toBeDisabled();
// Verify option count
await expect(page.getByLabel('Category').locator('option')).not.toHaveCount(0);Common mistakes
Using selectOption() on a custom dropdown. The call throws. Click the option or press keys instead.
Waiting Strategies in Playwright: No More sleep(). count() returns what is on the page right now, so a list that loads asynchronously can give 0. Use toHaveCount(), which retries.
Selecting by index. A new option added before yours shifts the index, and the test picks the wrong item. Use text or value.
Playwright Assertions: The Complete Guide. Assert the value the dropdown shows with toHaveValue() or toHaveText().
FAQ
How do I find the locator for a React Select?
The classes .react-select__control, __option, __single-value and __clear-indicator exist only if the app sets classNamePrefix="react-select". Without it, use getByRole('combobox') and getByRole('option'), which React Select v5 gives to its input and its options.
Should I select by text, value or index?
A string passed to selectOption('Germany') is matched against both the option's value and its label, so selectOption({ value: 'de' }) works too. An index shifts as soon as an option is added before yours, so prefer text or value.
How do I assert the option count of a dropdown that loads asynchronously?
count() reads the page once and can return 0 while the list is still loading. Use expect(locator).toHaveCount(n), or .not.toHaveCount(0) when any option will do, because both retry.
How do I fill a date field?
For an <input type="date">, call fill('1990-06-15') in yyyy-mm-dd format. If month, day and year are separate <select> elements, call selectOption() on each.
→ See also: Playwright Locators: Which One to Use and When | Playwright Assertions: The Complete Guide | Multi-Tab, Multi-Window, and iFrame Handling in Playwright | File Upload and Download in Playwright Tests