Search code examples
pythonplaywrightplaywright-python

How to find partial text using Playwright


I'm using Playwright to write automated tests.

My goal is to find an element by text contains and not by full match:

myElement = self.page.locator('text="Some Text 123"')

I wish to find only the elements with the text 123, how to do that?


Solution

  • So for text selector, there are two behaviours, that you can look into:

    1. text=Log in - default matching is case-insensitive and searches for a substring.
    2. text="Log in" - text body can be escaped with single or double quotes to search for a text node with exact content.

    So to search for a substring you can use the string without the double quotes:

    myElement = self.page.locator('text=Some Text 123')