Puppeteer CSS 选择器不适用于故事书?

发布于 2025-01-12 22:10:25 字数 493 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不念旧人 2025-01-19 22:10:25

Storybook 在 iframe 中呈现组件/元素,这意味着常规的 document.querySelector 无法找到它们。

它似乎在控制台中工作的原因是,一旦您激活 iframe,控制台就会切换 document 的上下文,例如通过单击其中的任意位置,或将焦点集中在其内部的元素。

要解决该问题,请像这样找到 iframe(此示例使用 DOM API,您需要找到 puppeteer 方法来执行此操作):

const iframe = document.querySelector('#storybook-preview-iframe');

然后,找到您的元素:

let el = iframe.contentDocument.querySelector('input[type=submit]');

这是在 puppeteer 中的样子:

const elementHandle = await page.waitForSelector('#storybook-preview-iframe');
const frame = await elementHandle.contentFrame();
await frame.waitForSelector('input[type=submit]');
const el = await frame.$('input[type=submit]');
el.click();

Storybook presents the components/elements inside an iframe, which means a regular document.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):

const iframe = document.querySelector('#storybook-preview-iframe');

Then, to find your element:

let el = iframe.contentDocument.querySelector('input[type=submit]');

Here's what it would look like in puppeteer:

const elementHandle = await page.waitForSelector('#storybook-preview-iframe');
const frame = await elementHandle.contentFrame();
await frame.waitForSelector('input[type=submit]');
const el = await frame.$('input[type=submit]');
el.click();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文