Skip to content
CodeKruPro

Select Element by Role

6/70

Select Element by Role

page.getByRole() finds elements by their ARIA role. This is the recommended Playwright locator because it reflects how users and assistive technology perceive the page.

Common roles: button, link, textbox, heading, checkbox, radio.

You can filter by accessible name: page.getByRole('button', { name: 'Submit' })

Read the button's id with locator.getAttribute('id').

Check whether it can be clicked with locator.isEnabled() — it returns true or false.

On the Buttons page, there is a button with text Left click on me (id: leftClick).

Task:

  1. Navigate to https://testkru.com/Elements/Buttons
  2. Find the button with getByRole(), using the button role and its visible text as the accessible name
  3. Print its id attribute, read with getAttribute()
  4. Print whether it is enabled using isEnabled()

Expected output:

Button ID: leftClick
Enabled: true