Selector-less login helper. Given a Playwright page, a username and password, locates the three login form elements (username field, password field, submit button) via a semantic-first waterfall of locator strategies so users don't have to hand-author CSS selectors when creating a project.
Strategies (in order, per field)
Username field
page.locator('input[type="email"]').first()page.getByLabel(/email|user|login/i)page.getByPlaceholder(/email|user|login/i)page.getByRole('textbox', { name: /email|user|login/i })page.locator('input[name*="email" i], input[name*="user" i], input[id*="email" i], input[id*="user" i]')- First visible non-password
<input>on the page (last resort).
Password field
page.locator('input[type="password"]').first()— almost always wins.
Submit button
page.getByRole('button', { name: /sign in|log in|login|submit|continue/i })page.locator('button[type="submit"], input[type="submit"]').first()form button:not([type="button"])scoped to the password field's form.- Fallback: press
Enterinside the password field (browsers submit the form natively).
Honest limitations: this is a best-effort heuristic, not an AI solver. It handles ~90% of conventional login pages (email + password + button) but will miss exotic flows (multi-step SSO, captchas, phone-number-first forms, shadow-DOM components without semantic roles). Those sites can still fall back to the recorder or legacy explicit selectors.
Backwards compatibility
Projects that already persist explicit usernameSelector / passwordSelector
/ submitSelector values continue to use them (fast path). This module is
only invoked when those fields are blank.
- Source:
Example
const ok = await performAutoLogin(page, {
username: "alice@example.com",
password: "secret",
}, { timeout: 5000, logger: (m) => console.log(m) });
Methods
(static) performAutoLogin(page, creds, optsopt) → {Promise.<object>}
Attempt to log in by auto-detecting the login form elements.
Parameters:
| Name | Type | Attributes | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
page |
object | Playwright |
||||||||||||||||
creds |
object |
|
||||||||||||||||
opts |
object |
<optional> |
Properties
|
- Source:
Returns:
Result envelope { ok: boolean, reason?: string }.
Never throws — transient Playwright errors are captured in reason.
- Type
- Promise.<object>