×
☰ See All Chapters

Type Selector in Puppeteer

If a tag is used only once in a web page, then it can be used for type selector. If there are multiple same tags, puppeteer always locates the first matching element.

Syntax

Example

tagName

select

selector.ts

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://localhost:8090/tools4testing/webfiles/contents/puppeteer/testpages/01puppeteer-selector-testpage.html');

    await selectGender(page);

    //wait for some time before closing, specify time in milliseconds

    await wait(5000);

    //Close browser

    await browser.close();

}

 

//Type/tag selector  

async function selectGender(page: Page) : Promise<void> {

    const gender = await page.$("select");

    await gender.focus();

    await gender.select("Male");

}

 

//wait if needed

async function wait(time) {

    return new Promise(function(resolve) {

        setTimeout(resolve, time)

    });

}

Click here to learn to execute puppeteer example using typescript

You can write the script and test above example using our below Test Page

type-selector-in-puppeteer-0
 

All Chapters
Author