Sure, you can pass a child locator as a parameter and click the child element without getting a 'strict mode violation' error in Playwright. You can do this using the locator.filter()
method.
Suppose you have this DOM structure:
<li><label>Hello</label></li>
<li><label>World</label></li>
And you want to target the parent <li>
of a label with text "Hello". You can use locator.filter()
with page.getByText('Hello')
like this:
const child = page.getByText('Hello');
const parent = page.getByRole('listitem').filter({ has: child });
This way, you're specifying that you want to find the parent element that contains the specified child element. This is more reliable than using XPath.
But if there's no suitable locator for the parent element or if changes in DOM structure might break your tests, you can use XPath with xpath=...
notation. Here's how:
const parent = page.getByText('Hello').locator('xpath=..');
Remember, React locator functionality is experimental and prefixed with _
. It might change in future versions of Playwright.
So, by using Playwright's locator.filter()
method or XPath notation (xpath=...
), you can pass a child locator as a parameter and click the child element without a 'strict mode violation' error.
If you still have questions, please ask a question and I will try to answer it.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].