question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

page.waitForSelector() with hidden === true always succeeds in some situations

See original GitHub issue
  • Puppeteer version: 1.20.0
  • Platform / OS version: macOS 10.14.6
  • URLs (if applicable): (private)
  • Node.js version: 12.7.0

What steps will reproduce the problem? If I run the following TypeScript code, Selector found will always be logged to the console no matter what selector is waited for.

import puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    headless: false,
    //slowMo: 100
  });
  const page = await browser.newPage();
  await page.goto('https://www.google.com/');
  const navigationPromise = page.waitForNavigation();
  //await navigationPromise // uncomment to fix
  await page.waitForSelector('any-text-you-like', { hidden: true, timeout: 0 });
  console.log('Selector found')
})();

This is not the case if hidden: true is changed to visible: true.

In my particular use case there may be pauses and more than one redirect before the correct webpage (which doesn’t have fixed URL) loads so I’m using waitForSelector() with a hidden element like <div id="my-secret-element-id" style="display: none;"> to detect when the correct web page is loaded. But this is not working due to this issue and awaiting e.g. page.waitForNavigation({'waitUntil': 'networkidle0'}); doesn’t solve the issue.

What is the expected result?

Console should not show Selector found unless the selector has actually been found.

What happens instead?

Console does show Selector found.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

3reactions
J-Rojascommented, Jun 4, 2020

It definitely works. I tested this rigorously after suspecting some errors, but it turns out I wasn’t handling race conditions very well. As suggested by @paperbackdragon, waitForSelector(..., { hidden: true }) may fail if the node is not in the DOM. You should proceed a call of hidden: true with a await waitForSelector(...) to ensure you are correctly waiting for the node to be visible before testing that it is hidden, if you expect it to appear then disappear within your test case.

You should also try await page.waitForNavigation({waitUntil: 'load'}) after navigation to ensure the DOM is fully parsed and available.

1reaction
paperbackdragoncommented, Feb 22, 2020

Reading the docs for waitForSelector, I see this bit from where it talks about hidden:

wait for element to not be found in the DOM or to be hidden

To me that means that using { hidden: true } will result in a success if the selector is not found on the page, meaning the element was not found in the DOM.

That said, if it’s not meant to work that way then I’d be interested to know, because I’ve been using it with that assumption.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Page.waitForSelector() method - Puppeteer
Promise which resolves when element specified by selector string is added to DOM. Resolves to null if waiting for hidden: true and selector...
Read more >
What happens when we call puppeteer waitForSelector API
That function will resolve a promise when the selector is visible (or hidden, depending on your options), or when it timeouts.
Read more >
Puppeteer documentation - DevDocs
For example, page.waitForSelector(selector[, options]) might fail if the selector doesn't match any nodes during the given timeframe.
Read more >
API Reference — Pyppeteer 0.0.25 documentation
Defaults to True unless appMode or devtools options is True . executablePath (str): Path to a Chromium or Chrome executable to run instead...
Read more >
Step by Step Guide to Web Scraping JavaScript Content using ...
It started as writing scripts to visit a website and extract necessary data after ... Websites used to be written with HTML, CSS,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found