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