'I'm getting an error using playwright nth-match

I just discovered playwright and I'm trying to see if it would be better to use than selenium. One issue I have is that none of the items in the application I write testing scripts for have names and this will not change. I'm trying to use nth-math with text to find the second instance of "login" to click the "button" on the front page, but I keep getting an error.

Line that throws an error:

page.locator(':nth-match(:text("Login"), 2').click()

I also tried

page.click(':nth-match(:text("Login"), 2')

Both of these throw the same error playwright._impl._api_types.Error: Unexpected token "" while parsing selector ":nth-match(:text("Login"), 2"



Solution 1:[1]

You are missing ) after 2:

page.locator(':nth-match(:text("Login"), 2').click()

Use:

await page.click(':nth-match(:text("Login"), 2)');

Here is another way to do it that I find cleaner and more readable: https://playwright.dev/docs/selectors#n-th-element-selector

So in You case

page.click('button:has-text("Login") >> nth = 2')

Solution 2:[2]

.NET syntax - this worked for me

await page.Locator(":nth-match(:text('your text'), 2)").ClickAsync();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 senaid
Solution 2 Suraj Rao