-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed
Description
Version
1.48.0
Steps to reproduce
- Call
locator.select_option(index=0)on a locator. - Call
locator.select_option(value="")on a locator.
Expected behavior
- The first option (index 0) is selected.
- The blank option (value "") is selected.
Actual behavior
- All options are unselected, leaving an empty <select>, as if
index=Noneis specified. - All options are unselected, leaving an empty <select>, as if
value=Noneis specified.
Additional context
The convert_select_option_values function is implemented incorrectly:
playwright-python/playwright/_impl/_element_handle.py
Lines 395 to 406 in c4df71c
| if value: | |
| if isinstance(value, str): | |
| value = [value] | |
| options = (options or []) + list(map(lambda e: dict(valueOrLabel=e), value)) | |
| if index: | |
| if isinstance(index, int): | |
| index = [index] | |
| options = (options or []) + list(map(lambda e: dict(index=e), index)) | |
| if label: | |
| if isinstance(label, str): | |
| label = [label] | |
| options = (options or []) + list(map(lambda e: dict(label=e), label)) |
The value, index, and label should be checked for None, not its truthiness.
Minimal reproducer:
import base64
from playwright.sync_api import sync_playwright
TEST_HTML = b'''
<select>
<option value="A">Option A</option>
<option value="B">Option B</option>
<option value="">Empty Option</option>
</select>
'''
with sync_playwright() as playwright:
browser = playwright.chromium.launch() # or firefox, or webkit
page = browser.new_page()
page.goto('data:text/html;base64,' + base64.b64encode(TEST_HTML).decode())
index_choice = page.locator("select").select_option(value="A")
assert index_choice == ["A"], index_choice # OK
index_choice = page.locator("select").select_option(value="B")
assert index_choice == ["B"], index_choice # OK
index_choice = page.locator("select").select_option(index=2)
assert index_choice == [""], index_choice # OK
index_choice = page.locator("select").select_option(index=0)
assert index_choice == ["A"], index_choice # FAIL
index_choice = page.locator("select").select_option(value="")
assert index_choice == [""], index_choice # FAILEnvironment
- Operating System: [Ubuntu 24.04]
- CPU: [x86_64]
- Browser: [All]
- Python Version: [3.12.3]
Metadata
Metadata
Assignees
Labels
No labels