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:
- Navigate to
https://testkru.com/Elements/Buttons - Find the button with
getByRole(), using thebuttonrole and its visible text as the accessible name - Print its
idattribute, read withgetAttribute() - Print whether it is enabled using
isEnabled()
Expected output:
Button ID: leftClick Enabled: true
TestKru