Search code examples
pythonplaywrightplaywright-python

How to find element by attribute and text in a singe locator?


How can I find an element using Playwright using a single locator phrase?

My element is:

<div class="DClass">Hello</div>

I wish to find the element by its class and text:

myElement = self.page.locator('text="Hello",[class="DClass"]')

Why it does not work?


Solution

  • If you separate the selectors with a , that's an or. You can chain selectors using >>.

    myElement = self.page.locator('text="Hello" >> [class="DClass"]')