☰ See All Chapters |
Puppeteer handling checkbox
The “click()” method is used to click on Buttons, Links and checkbox (and other web elements). You can select and deselect the check box using the “click()” method of the “ElementHandle” class.
const newsletter = await page.$("#newsletter"); newsletter.click(); |
To check whether check box is selected, read the checked property of ElementHandle.
const isCheckBoxChecked = await (await newsletter.getProperty("checked")).jsonValue(); console.log(isCheckBoxChecked); |
You can write the script and test these using our Test Page
Puppeteer checkbox example
import { launch, Page } from 'puppeteer'; example(); async function example() { const browser = await launch({headless : false}); const page = await browser.newPage(); await page.setViewport({ width: 1366, height: 768}); await page.goto('https://www.tools4testing.com/contents/puppeteer/testpages/registration-form-testpage'); await enterDetails(page); //Close browser await browser.close(); }
async function enterDetails(page: Page) : Promise<void> { const newsletter = await page.$("#newsletter"); newsletter.click(); const isCheckBoxChecked = await (await newsletter.getProperty("checked")).jsonValue(); console.log(isCheckBoxChecked); } |
Click here to learn to execute puppeteer example using typescript
All Chapters