Skip to content

[Bug]: select_option(index=0) incorrectly interpreted as select_option(index=None) #2659

@jinohkang-theori

Description

@jinohkang-theori

Version

1.48.0

Steps to reproduce

  1. Call locator.select_option(index=0) on a locator.
  2. Call locator.select_option(value="") on a locator.

Expected behavior

  1. The first option (index 0) is selected.
  2. The blank option (value "") is selected.

Actual behavior

  1. All options are unselected, leaving an empty <select>, as if index=None is specified.
  2. All options are unselected, leaving an empty <select>, as if value=None is specified.

Additional context

The convert_select_option_values function is implemented incorrectly:

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 # FAIL

Environment

- Operating System: [Ubuntu 24.04]
- CPU: [x86_64]
- Browser: [All]
- Python Version: [3.12.3]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions