I wonder how I can access an element by ID. I want to submit a form.
await page.click("id= 'next'");
--> not possibleawait page.getByRole('button', { id: 'next' }).click();
--> does not compileawait page.getByRole('button', { name: 'Sign in' }).click();
--> work but is language dependentSelecting elements by their ID seems the most robust to me. Am I missing something?
You can do something like this:
await page.locator("#YourId").click()
or just
await page.click("#YourId");
Playwright recommends some predefined locators so you could also check if some off those fit your use case.
What you are missing in the first example are the square brackets, instead of:
await page.click("id= 'next'");
you should do this:
await page.click("[id='next']");
alternatively, you can use the shorthand version like mentioned in the other answer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With