Multi-select dropdown list in Playwright Python

Tutorial 10​​ ​​ Multi-select dropdown list​​ in Playwright​​ Python​​ 

What you will Learn​​ in this​​ blog:

  • Select multiple options in drodpwn list

  • Select multiple options in drodpwn list (label option)

  • Select multiple options in drodpwn list (index, value option)

  • Code snippet

Select multiple options in drodpwn list

Launch​​ https://selenium08.blogspot.com/2019/11/dropdown.html​​ 

Notice that we can select mutiple options from the list

Let us see how to do that using playwright​​ python.

Let us inspect the dropdown,​​ notice that​​ it is represented by ‘select’​​ tag and has​​ the unique name ‘Month’​​ 

If you​​ read​​ the​​ ‘select_option’ method documentation,​​ it mentions the syntax usage to select multiple options

So​​ we can write like below

Save and execute

Notice that multiple months get selected

Select multiple options in drodpwn list (label option)

Let us now see how to use ‘label’ to select an option

The usage is as shown below

Save and execute.

Notice that multiple months get selected including ‘March’

Select multiple options in drodpwn list (index, value option)

Let us now see how to use ‘index’ and ‘value’ to select an option

Save and execute.

Notice that multiple months get selected (index=2 represents 2nd​​ month)

So this is how we can work with multi select dropdown lists.

Code snippet​​ (multi select)

import​​ time

from​​ playwright.sync_api​​ import​​ Playwright,​​ sync_playwright,​​ expect


def​​ run(playwright: Playwright) ->​​ None:
 ​​ ​​ ​​​​ browser = playwright.chromium.launch(
headless=False,​​ slow_mo=3000)
 ​​ ​​ ​​​​ context = browser.new_context()


 ​​ ​​ ​​​​ page = context.new_page()

 ​​ ​​ ​​​​ page.goto(
"https://selenium08.blogspot.com/2019/11/dropdown.html")

 ​​ ​​ ​​​​ 
#page.locator("select[name='Month']").select_option(['Sept', 'May', 'July'])

 ​​ ​​ ​​​​ #page.locator("select[name='Month']").select_option(['Sept', 'May'], label='March')


 ​​ ​​ ​​​​ 
page.locator("select[name='Month']").select_option(index=2,​​ value='May',​​ label='October')

 ​​ ​​ ​​​​ 
# ---------------------
 ​​ ​​ ​​​​ 
context.close()
 ​​ ​​ ​​​​ browser.close()



with​​ sync_playwright()​​ as​​ playwright:
 ​​ ​​ ​​​​ run(playwright)

​​ 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 10000 RS/149 USD
Attend All Live courses in just 10000 rs / $149 - offer ends 31st May 2024