javascript - 检查视口(viewport)中的可见性( puppeteer 师)

标签 javascript cucumber puppeteer

如何使用 Puppeteer 检查 HTML 元素是否在视口(viewport)(可见区域)内可见?例如,如果我必须滚动才能看到一个按钮,那么它是不可见的。

page.waitForSelector('#element', { visible: true })

page.waitForSelector 函数不是我要找的,因为它只检查元素是否在 DOM 中并且没有被 CSS 属性隐藏。

有什么方法可以检查元素在视口(viewport)中的可见性吗?

最佳答案

您可以使用 elementHandle.isIntersectingViewport() 检查元素在当前视口(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.
}

关于javascript - 检查视口(viewport)中的可见性( puppeteer 师),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48663356/

相关文章:

javascript - 页面更改后的剑道网格事件

javascript - 在 Android Webview 中嵌入 Facebook 帖子

javascript - 仅在 Jenkins 上运行 Cucumber 的 javascript 表单超时

java - 在 Cucumber 测试场景中比较整数值的最佳实践

javascript - 在加载 DOM 时创建自定义 HTML 标签

javascript - window.onload 函数在 Mozilla Firefox 上不起作用

testing - 如何跳过不同环境的测试场景

javascript - 错误 : EPERM: operation not permitted while running puppeteer JavaScript node

ffmpeg - 让 ffmpeg 捕获运行 puppeteer 脚本的 xfvb-run screen 的全屏,并通过 rtmp 发送

javascript - WaitForTarget 上的 Puppeteer 超时