Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading