Skip to main content
  1. Blog/

Selecting elements matching one of the conditions in Playwright

·98 words·1 min
Lucas Melin
Author
Lucas Melin

In working with Playwright, sometimes you need to search for an element like a button or dropdown that might have one of several attributes.

The :is() pseudo-class is an experimental CSS pseudo-class. It is a function that takes a selector list as its argument, and selects any element that can be selected by one of the selectors in that list. This is useful for writing large selectors in a more compact form.

For example:

@when('I select any reason from the dropdown')
def step_impl(context):
    page = context.browser.page
    reason_one = "Reason One"
    reason_two = "Reason Two"
    page.click(f":is([data-assoc-field={reason_one}], [data-assoc-field={reason_two}]) > a")