Skip to content
Advertisement

Puppeteer: waitForSelector followed by click is not working

I am trying to scrape a dynamic site using puppeteer in node, but not able to click the required elements no matter what. Please help!

// this script parses the data from https://excise.wb.gov.in/CHMS/Public/Page/CHMS_Public_Hospital_Bed_Availability.aspx
const puppeteer = require('puppeteer');

(async function scrape() {
  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();
  // page.setDefaultNavigationTimeout(90000);
  const url = "https://excise.wb.gov.in/CHMS/Public/Page/CHMS_Public_Hospital_Bed_Availability.aspx";
  await page.goto(url, {waitUntil: 'networkidle2', timeout: 0});
  // await page.waitForNavigation({ waitUntil: 'networkidle2' })
  await page.waitForSelector('#ctl00_ContentPlaceHolder1_ddl_District');
  await page.click('#ctl00_ContentPlaceHolder1_ddl_District');
})();

Tried this too, but no luck:

  const selectButt = await page.evaluateHandle(() =>
    document.querySelector('#ctl00_ContentPlaceHolder1_ddl_District')
  );
  await selectButt.click();

Advertisement

Answer

It’s a <select>, you probably don’t want to click on it, but choose an option instead:

await page.select('#ctl00_ContentPlaceHolder1_ddl_District', '020'); // ALIPURDUAR

Docs for page.select

Triggers a change and input event once all the provided options have been selected.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement