Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save pablocattaneo/5c6b2e8b6a5a6fb9185e10fd8b860a14 to your computer and use it in GitHub Desktop.

Select an option

Save pablocattaneo/5c6b2e8b6a5a6fb9185e10fd8b860a14 to your computer and use it in GitHub Desktop.
Selects one or multiple options in the <select> element with locator.selectOption(). You can specify option value, or label to select. Multiple options can be selected.
// Single selection matching the value or label
await page.getByLabel('Choose a color').selectOption('blue');
// Single selection matching the label
await page.getByLabel('Choose a color').selectOption({ label: 'Blue' });
// Multiple selected items
await page.getByLabel('Choose multiple colors').selectOption(['red', 'green', 'blue']);
// Example
import { test } from "@playwright/test";
const PAGE_URL = "https://testautomationpractice.blogspot.com/";
test("Single select dropdown actions", async ({ page }) => {
await page.goto(PAGE_URL);
const dropdown = page.getByRole("listbox").and(page.locator("#country"));
await dropdown.selectOption("India");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment