Search code examples
pythonplaywrightplaywright-python

Using Playwright with CSS that contains nth element


I'm trying to find an element with a specific nth index as a CSS expression.

How can I fix my code without changing the CSS expression?

try:
    expect(self.page.locator('div[class="some-class"]:nth(3)')).\
                   to_be_visible(timeout=20000)
    return True
except AssertionError:
    return False

The error I get is:

{Error}DOMException: Failed to execute 'querySelectorAll' on 'Document': 'div[class="some-class"]:nth(3)' is not a valid selector.


Solution

  • Your locator should look like this. Replace ":" with ">>"

    div[class="some-class"]>>nth=3