waitForSelector with visible:true not returning the first visible element, causes timeout.
See original GitHub issueSteps to reproduce
Tell us about your environment:
- Puppeteer version: 0.14.0
- Platform / OS version: MacOS (Dockerized in node:10)
- URLs (if applicable): https://j4q389wzv3.codesandbox.io/
- Node.js version: v10.15.3
What steps will reproduce the problem? Open Website OR use this file:
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<input style="display: none" type="number" class="my-input-class" />
<input type="number" class="my-input-class" />
</body>
</html>
Please include code that reproduces the issue.
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://j4q389wzv3.codesandbox.io/');
try {
const visibleInput = await page.waitForSelector('.my-input-class', {visible: true, timeout: 1000 })
console.log('Found visible Element')
} catch (e) {
console.log('Could NOT find a visible element ', e.message)
}
const inputs = await page.$$('.my-input-class')
console.log(`Found ${inputs.length} inputs`)
await browser.close()
What is the expected result?
There are 2 elements matching the CSS Selector on the page. the first one is hidden, the second one is visible. The page.waitForSelector
with {visible: true}
should have found and returned the visible element on the page.
What happens instead? It Times Out since there was another HIDDEN element matching the CSS selector higher up in the DOM structure, causing it to wait until timeout even though a visible element exists on the page.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:16 (2 by maintainers)
Top Results From Across the Web
puppeteer: how to wait until an element is visible?
I think you can use page.waitForSelector(selector[, options]) function for that purpose. const puppeteer = require('puppeteer'); ...
Read more >Page.waitForSelector() method - Puppeteer
Wait for the selector to appear in page. If at the moment of calling the method the selector already exists, the method will...
Read more >Puppeteer documentation - DevDocs
The method runs document.querySelector within the page. If no element matches the selector, the return value resolves to null . Shortcut for page.mainFrame()....
Read more >API Reference — Pyppeteer 0.0.25 documentation
Non visible pages, such as "background_page" , will not be listed here. ... If browser instance is created by pyppeteer.launcher.connect() , return None...
Read more >How to make puppeteer wait for page to load - Urlbox
We can use Page.waitForSelector() like so, the {visible: true} option tells Puppeteer to wait for the element to be present in the DOM...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
A working solution (though, not ideal) is as follows. Ideally the waitForSelector with { visible:true } would behave this way… or the documentation clearly states that it doesn’t do this.
@razorman8669 Ok I see what you mean here. Indeed, the behavior is somewhat confusing.
However, I disagree with the proposed approach. CSS selector is the only thing that we should use to identify the element. Adding the suggested logic adds magic to how we find elements - making it equally hard to debug.
The best solution here is to update the documentation - so that we do a better job explaining what’s going on.