Locate inputs by their label
beginner Locators Challenge 3 of 22
Write a locator for the Password input of the sign-up form. The form also has a “Confirm password” field, and the help text below mentions “password” too.
Requirements
- Use
page.getByLabel(...). - Resolve to exactly one element: the input labelled “Password”.
- Do not use
page.locator(),.nth(),.first()or.last().
Expected output
Resolves to exactly 1 element: the "Password" input of the sign-up form (not "Confirm password").
Checks (4)
- ○Resolves to exactly the intended element(s)
- ○Uses getByLabel
- ○Does not use a CSS or XPath locator
- ○Does not rely on position (nth, first or last)
example.spec.tsLocator engine · not a real Playwright run
Ctrl/⌘ + Enter to check locator · your code is saved in this browser
Results
Check your locator to see which elements it resolves to.
Page under test
matches are outlined in greenView page HTML
<form id="signup" class="card">
<h2>Create your account</h2>
<label for="full-name">Full name</label>
<input id="full-name" name="fullName" type="text">
<label for="signup-email">Email</label>
<input id="signup-email" name="email" type="email">
<label class="field">
Password
<input name="password" type="password" autocomplete="new-password">
</label>
<label class="field">
Confirm password
<input name="confirmPassword" type="password" autocomplete="new-password">
</label>
<p class="help">Your password must be at least 12 characters.</p>
<button type="submit">Create account</button>
</form>
<form id="newsletter">
<label for="nl-email">Email for our newsletter</label>
<input id="nl-email" type="email">
<button type="submit">Subscribe</button>
</form>