How can I check with Puppeteer whether an HTML element is visible within the viewport (the visible area) or not? For example if I have to scroll to be able to see a button, then it's not visible.
page.waitForSelector('#element', { visible: true })
The page.waitForSelector function is not what I'm looking for, because it only checks if the element is in the DOM and is not hidden with CSS properties.
Is there any method to check an element's visibility in the viewport?
You can use elementHandle.isIntersectingViewport()
to check if an element is visible within the current viewport:
const example = await page.$('#example');
if (await example.isIntersectingViewport()) {
// The element IS visible within the current viewport.
} else {
// The element IS NOT visible within the current viewport.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With