Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jinohkang-theori opened this issue Nov 21, 2024 · 1 comment · Fixed by #2661
Closed

Comments

@jinohkang-theori
Copy link

jinohkang-theori commented Nov 21, 2024

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]
@jinohkang-theori jinohkang-theori changed the title [Bug]: select_option(index=0) incorrectly treated as select_option(index=None) [Bug]: select_option(index=0) incorrectly interpreted as select_option(index=None) Nov 21, 2024
@mxschmitt
Copy link
Member

Thank you for the great bug report and investigation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants