×
☰ See All Chapters

Puppeteer handling radio button

The “click()” method is used to click on Buttons, Links and checkbox (and other web elements). You can select and deselect the radio buttons using the “click()” method of the “ElementHandle” class.

const mgender = await page.$("#mgender");

await mgender.click();

To check whether radio button is selected, read the checked property of ElementHandle.

const isRadioSelected = await (await mgender.getProperty("checked")).jsonValue();

console.log(isRadioSelected);

You can write the script and test these using our Test Page

puppeteer-handling-radio-button-0
 

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 mgender = await page.$("#mgender");

    await mgender.click();

    const isRadioSelected = await (await mgender.getProperty("checked")).jsonValue();

    console.log(isRadioSelected);

}

Click here to learn to execute puppeteer example using typescript


All Chapters
Author