Puppeteer CSS 选择器不适用于故事书?
const puppeteer = require('puppeteer');
async function run () {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
await page.$eval('input[type=submit]', el => el.click());
await page.screenshot({path: 'screenshot.png'});
browser.close();
}
run();
当我在任何网站上运行此命令时,例如 '节点截图.js“https://github.com/login”' 它按预期工作,但对于故事书,它根本找不到元素,即使我无法从控制台中选择它们。故事书有什么不支持木偶师的吗?是否有其他工具可以在无头浏览器中悬停/单击故事书组件?
const puppeteer = require('puppeteer');
async function run () {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
await page.$eval('input[type=submit]', el => el.click());
await page.screenshot({path: 'screenshot.png'});
browser.close();
}
run();
When I run this with any site like
'node screenshot.js "https://github.com/login"'
it works as expected but with storybook, it simply can't find elements even tho I can't select them from the console fine. Is there something with storybook not supporting puppeteer? Is there a different tool to hover/click on storybook components in headless browser?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Storybook 在
iframe
中呈现组件/元素,这意味着常规的document.querySelector
无法找到它们。它似乎在控制台中工作的原因是,一旦您激活 iframe,控制台就会切换
document
的上下文,例如通过单击其中的任意位置,或将焦点集中在其内部的元素。要解决该问题,请像这样找到 iframe(此示例使用 DOM API,您需要找到 puppeteer 方法来执行此操作):
然后,找到您的元素:
这是在 puppeteer 中的样子:
Storybook presents the components/elements inside an
iframe
, which means a regulardocument.querySelector
cannot find them.The reason it seems to work in the console is that the console switches context for
document
as soon as you activate the iframe, e.g. by clicking anywhere inside of it, or focussing an element inside of it.To solve the issue, find the iframe like this (this example uses the DOM API, you need to find the puppeteer way to do this):
Then, to find your element:
Here's what it would look like in puppeteer: