From 2a1dd36d71bca251414f048241f6f0a5c0c56995 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 14 Apr 2026 14:26:15 -0700 Subject: [PATCH] fix(selectOption): match label with html whitespace Fixes https://github.com/microsoft/playwright/issues/40210 --- packages/injected/src/injectedScript.ts | 6 ++++-- tests/page/page-select-option.spec.ts | 11 +++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/injected/src/injectedScript.ts b/packages/injected/src/injectedScript.ts index 9f28be81af056..510c7e7d56695 100644 --- a/packages/injected/src/injectedScript.ts +++ b/packages/injected/src/injectedScript.ts @@ -786,16 +786,18 @@ export class InjectedScript { let remainingOptionsToSelect = optionsToSelect.slice(); for (let index = 0; index < options.length; index++) { const option = options[index]; + const normalizedOptionLabel = normalizeWhiteSpace(option.label); const filter = (optionToSelect: Node | { valueOrLabel?: string, value?: string, label?: string, index?: number }) => { if (optionToSelect instanceof Node) return option === optionToSelect; + const matchesLabel = (label: string) => label === option.label || normalizeWhiteSpace(label) === normalizedOptionLabel; let matches = true; if (optionToSelect.valueOrLabel !== undefined) - matches = matches && (optionToSelect.valueOrLabel === option.value || optionToSelect.valueOrLabel === option.label); + matches = matches && (optionToSelect.valueOrLabel === option.value || matchesLabel(optionToSelect.valueOrLabel)); if (optionToSelect.value !== undefined) matches = matches && optionToSelect.value === option.value; if (optionToSelect.label !== undefined) - matches = matches && optionToSelect.label === option.label; + matches = matches && matchesLabel(optionToSelect.label); if (optionToSelect.index !== undefined) matches = matches && optionToSelect.index === index; return matches; diff --git a/tests/page/page-select-option.spec.ts b/tests/page/page-select-option.spec.ts index 8b6afbd640d61..6873fc01eaa99 100644 --- a/tests/page/page-select-option.spec.ts +++ b/tests/page/page-select-option.spec.ts @@ -48,6 +48,17 @@ it('should select single option by label', async ({ page, server }) => { expect(await page.evaluate(() => window['result'].onChange)).toEqual(['indigo']); }); +it('should select single option by label with html whitespace', async ({ page }) => { + await page.setContent(` + + `); + await page.selectOption('select', { label: 'HTML' }); + expect(await page.evaluate(() => document.querySelector('select').value)).toBe('html'); +}); + it('should select single option by handle', async ({ page, server }) => { await page.goto(server.PREFIX + '/input/select.html'); await page.selectOption('select', await page.$('[id=whiteOption]'));