c0nfus3d
c0nfus3d

Reputation: 1440

Click checkbox with puppeteer via element ID

I have this puppeteer code:

(async () => {
  const browser = await puppeteer.launch({ args: ['--no-sandbox'] });
  const page = await browser.newPage();

  await page.goto("https://myurl.com/page");
  await page.waitForSelector("#select-all-checkbox");

  var bodyHTML = await page.content();
  console.log(bodyHTML + "\n\n");

  await page.click("#select-all-checkbox");
  await page.close();
  await browser.close();
})();

Logging the HTML to the console, I have verified the page I am accessing has this HTML:

<label><input type="checkbox" name="" id="select-all-checkbox" value="" checked=""><span class="ifaFs"><span data-testid="icon-checkbox-someselected" class="hdDWuD"></span></span></label>

I am receiving this error on the page.click line:

(node:3827) UnhandledPromiseRejectionWarning: Error: Node is either not visible or not an HTMLElement
at ElementHandle._clickablePoint (/path/to/node_modules/puppeteer/lib/JSHandle.js:217:13)
at process._tickCallback (internal/process/next_tick.js:68:7)
-- ASYNC --
at ElementHandle. (/path/to/node_modules/puppeteer/lib/helper.js:111:15)
at DOMWorld.click (/path/to/node_modules/puppeteer/lib/DOMWorld.js:367:18)
at process._tickCallback (internal/process/next_tick.js:68:7)
-- ASYNC --
at Frame. (/path/to/node_modules/puppeteer/lib/helper.js:111:15)
at Page.click (/path/to/node_modules/puppeteer/lib/Page.js:1037:29)

Upvotes: 9

Views: 23244

Answers (2)

Israel
Israel

Reputation: 1224

This approach worked for me:

(async () => {
  const browser = await puppeteer.launch({ args: ['--no-sandbox'] });
  const page = await browser.newPage();

  await page.goto("https://myurl.com/page");
  const checkboxEl = await page.waitForSelector("#select-all-checkbox");
  await checkboxEl.click();

  await page.close();
  await browser.close();
})();

Upvotes: 1

c0nfus3d
c0nfus3d

Reputation: 1440

With my code example above, this was how I resolved the problem.

(async () => {
  const browser = await puppeteer.launch({ args: ['--no-sandbox'] });
  const page = await browser.newPage();

  await page.goto("https://myurl.com/page");
  await page.waitForSelector("#select-all-checkbox");

  await page.evaluate(() => {
    document.querySelector("#select-all-checkbox").parentElement.click();
  });

  await page.close();
  await browser.close();
})();

Upvotes: 10

Related Questions