Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Playwright for Python, how do I select an option from a drop down list?

This is a followup to this question on the basic functionality of Playwright for Python.

How do I select an option from a drop down list?

This example remote controls a vuejs-webseite that has a drop down list of fruits like "Apple", "Banana", "Carrot", "Orange"

Here I want to select the option "Banana"

from playwright import sync_playwright
import time

URL = '<my url>'

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False)
    page = browser.newPage()
    page.goto(URL)

    # identify this element by ID. Wait for it first
    new_selector = 'id=name-fruit'
    page.waitForSelector(new_selector)
    handle = page.querySelector(new_selector)

    # at this point I have the element and can print the content
    print(handle.innerHTML())

The drop down list HTML like this

<select data-v-c2cef47a="" id="name-fruit" autocomplete="true" class="form-select__select">
    <option data-v-c2cef47a="" disabled="disabled" value=""><!----></option>
    <option data-v-c2cef47a="" value="[object Object]"> Apple </option>
    <option data-v-c2cef47a="" value="[object Object]"> Banana </option>
    <option data-v-c2cef47a="" value="[object Object]"> Carrot </option>
    <option data-v-c2cef47a="" value="[object Object]"> Orange </option> 
</select>

in Selenium, I would select an option like this

from selenium.webdriver.support.ui import Select
Select(handle).select_by_visible_text('Banana')  # Note: no spaces needed!

The Javascript docs for Playwright have this, which is not exactly the same, because it seems to identify the object at the same time.

// Single selection matching the label
await page.selectOption('select#colors', { label: 'Blue' });

How do I do the selection in Playwright for Python?

I tried both of these and nothing happens.

handle.selectOption("text=Banana") 
handle.selectOption("text= Banana ")
like image 449
576i Avatar asked Oct 11 '20 16:10

576i


People also ask

How do you select a dropdown value in playwright?

const dropdown = await page. $('#dropdown'); In Playwright, we can use value to select the option, or we can use the label, or we can use the index.

How to select option by label in playwright?

In Playwright, we can use value to select the option, or we can use the label, or we can use the index. So, let's do something, let's first select by label. Here, await dropdown.selectOption and we want to select be label. So, we have to pass the label here.

How to work with drop-down list?

Working with Drop-down list: Initially, you have to import the Select class and afterward you have to make the case of Select class. After making the case of Select class, you can perform select strategies on that occasion to choose the choices from the dropdown list.

Does playwright work with the newer versions of Python?

The accepted answer does not work with the newer versions of Playwright. (Thanks @576i for pointing this out) Here is the Python code that works with the newer versions (tested with version 1.5):

How to get only the text of the playwright's text?

To get only the text, use the inner_text () function. At the time of writing the question, the accepted answer was working Python code. Since then, playwright has changed a bit and now uses a more Python friendly notation. (This is the case for many other older playwright questions) Got it. Thanks. I am editing this answer to reflect this.


3 Answers

After trying many different variants, I guessed a working syntax

handle.selectOption({"label": "Banana"})
like image 97
576i Avatar answered Sep 24 '22 00:09

576i


A working example for Playwright for Python:

page.select_option('select#colors', label='Banana')

or for JavaScript:

await page.selectOption('select#colors', { label: 'Banana' });

See here for further handling of how to interact with selectors and how to use it in different languages and scenarios like JavaScript.

like image 34
Max Schmitt Avatar answered Sep 23 '22 00:09

Max Schmitt


You can also alternatively use the index or the value to select an option:

handle.selectOption([{'label': 'Banana'}])  # selects 'Banana'
handle.selectOption([{'index': 3}])         # selects 'Carrot'
handle.selectOption([{'value': ''}])        # selects the empty option (works even though it is disabled)
like image 41
buddemat Avatar answered Sep 26 '22 00:09

buddemat