RadioButtons
While Selecting radio button there are multiple parameter you can pass to check method
Locator.check(options?: {
force?: boolean | undefined;
noWaitAfter?: boolean | undefined;
position?: {
x: number;
y: number;
} | undefined;
timeout?: number | undefined;
trial?: boolean | undefined;
} | undefined): Promise
Ensure that checkbox or radio element is checked.
Locator.check() is a Playwright method used to ensure a checkbox or radio button is checked.
Syntax
await locator.check(options);
It works only on:
<input type="checkbox">
<input type="radio">
- If the element is already checked, Playwright does nothing.
What happens internally?
When you call:
await page.getByLabel('Subscribe').check();
Playwright:
- Ensure that element is a checkbox or a radio input. If not, this method throws. If the element is already checked, this method returns immediately.
- Wait for actionability checks on the element, unless force option is set.
- Scroll the element into view if needed.
- Use page.mouse to click in the center of the element.
Ensure that the element is now checked. If not, this method throws.
If the element is detached from the DOM at any moment during the action, this method throws.
Waits for the element to be attached to the DOM.
Verifies it's visible and enabled.
Scrolls it into view if necessary.
Clicks it if needed.
Verifies the final state is checked.
Throws an error if it cannot become checked.
Options
force
await locator.check({ force: true });
Bypasses Playwright's actionability checks.
Normally Playwright ensures the element is:
visible
enabled
not covered by another element
With force: true, Playwright skips those checks and tries the action anyway.
Use sparingly, usually when dealing with custom UI components.
timeout
await locator.check({ timeout: 10000 });
Maximum time (in milliseconds) to wait for the action.
Example:
await locator.check({ timeout: 5000 });
Wait up to 5 seconds before failing.
position
await locator.check({
position: { x: 10, y: 10 }
});
Clicks at a specific coordinate within the element.
Useful when:
an element has unusual click handling
a custom checkbox only responds to clicks in a specific area
Example:
await locator.check({
position: { x: 5, y: 5 }
});
trial
await locator.check({ trial: true });
Performs all actionability checks without actually clicking.
Useful to verify whether Playwright could interact with the element.
Example:
await checkbox.check({ trial: true });
console.log('Checkbox is actionable');
No state change occurs.
noWaitAfter
await locator.check({ noWaitAfter: true });
Historically controlled waiting after actions that trigger navigation.
For modern Playwright versions this option is generally ignored/deprecated and rarely needed.
Checkbox Example
const checkbox = page.getByRole('checkbox', {
name: 'Accept Terms'
});
await checkbox.check();
await expect(checkbox).toBeChecked();
Radio Button Example
const radio = page.getByRole('radio', {
name: 'Male'
});
await radio.check();
await expect(radio).toBeChecked();
