Search code examples
playwrightplaywright-python

Playwright finds too much elements by text


I tried to find all the elements that has the text "Operation" inside them. I only see one such element visible, but Playwright is giving me 19:

 self.page.locator('*', has_text='Operation').click()

When I run this selector in the developer tools:

$x('//*[contains( text(),"Operation" )]')

I see only a single element.

What am I doing wrong here?


Solution

  • You can use the text locator and also add more Texts along with Operation to pinpoint your element more accurately.

    page.locator("text=Operation").click()
    page.locator("text=Some more words Operation").click()
    

    Or, you can also use the Nth element selector if you know the position of the text occurrence. Starts from 0

    page.locator("text=Operation >> nth=0").click()