Playwright + Java · Lesson 17 of 38
Module 17: Locators
Your task
Write a method getSubmitButton:
Write it in the editor, then press Check answer (Ctrl + Enter).
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit"))page.getByLabel("Email Address")page.getByPlaceholder("Enter your password")page.getByText("Welcome back")page.getByTestId("checkout-btn")import com.microsoft.playwright.*;
import com.microsoft.playwright.options.AriaRole;
Locator submitBtn = page.getByRole(AriaRole.BUTTON,
new Page.GetByRoleOptions().setName("Confirm Payment"));Locators are the heart of reliable Playwright automation. Playwright locators are lazy and automatically resolve every time you interact with them. In this lesson, we prioritize user-facing accessibility locators like getByRole and getByLabel over fragile CSS and XPath selectors. Let's master locators!
Part of the Playwright + Java course (Playwright with Java). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.
In Playwright, if a locator matches multiple elements and you call click() or fill(), it throws a strict mode violation error, catching unintended UI ambiguities.
Ready? Solve the task in the editor
Use Check answer to mark it complete. Hints are there if you need them.
___