Search code examples
playwright

How can I select an element by Id?


I wonder how I can access an element by ID. I want to submit a form.

  • await page.click("id= 'next'"); --> not possible
  • await page.getByRole('button', { id: 'next' }).click(); --> does not compile
  • await page.getByRole('button', { name: 'Sign in' }).click(); --> work but is language dependent

Selecting elements by their ID seems the most robust to me. Am I missing something?


Solution

  • 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