尝试使用 puppeteer 单击 iframe 中的按钮 [英] Trying to click a button within an iframe with puppeteer

查看:173
本文介绍了尝试使用 puppeteer 单击 iframe 中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用 puppeteer 进行一些轻型自动化.我在尝试单击 iframe 中的按钮时遇到问题.

This is my first time using puppeteer for some light automation. I am running into an issues trying to click a button that is located within an iframe.

这是我的代码:

await page.waitForSelector('iframe');
        const frameElement = await page.$(
            'iframe[src="https://ibx.key.com/ibxolb/login/client/index.html"]',
        );
        const frame = await frameElement.getFrame();

getFrame() 错误,因为 frameElement 作为 JSHandle@node 返回.

The getFrame() errors because the frameElement is return as a JSHandle@node.

我可以访问 iframe 的选择器是 srctitle.如您所见,我正在使用 src.

The selectors I have access to for the iframe are src and title. As you can see I am using src.

我已经搜索并搜索了如何执行此操作,但似乎没有一个示例适合我.

I have searched and searched for how to do this but none of the examples seem to work for me.

我被困住了,所以如果有人有任何建议、解决方案或方向,我将不胜感激.

I am stuck so if anyone has any advice, solution, or direction I would appreciate it.

谢谢,

推荐答案

如果您从 *.key.com*.keybank.com 旁边的 URL/站点访问 iframe,那么你就因为 CSP 指令无法打开 iframe.

If you're accessing the iframe from URL / sites beside *.key.com or *.keybank.com, then you can't open the iframe because of CSP directive.

打开 chrome 开发者控制台,如果您能看到此消息,那是因为 CSP 指令不允许您访问 iframe 站点 URL.

Open chrome developer console and if you can see this message, then it's because CSP directive not allowing you to access the iframe site URL.

拒绝显示'https://ibx.key.com/ibxolb/login/client/index.html' 在框架中,因为祖先违反了以下内容安全策略指令:frame-ancestors *.key.com *.keybank.com".

Refused to display 'https://ibx.key.com/ibxolb/login/client/index.html' in a frame because an ancestor violates the following Content Security Policy directive: "frame-ancestors *.key.com *.keybank.com".

如果你没有看到上面的任何消息,你可以试试这个

And if you're not seeing any message like above, you can try like this

const frame = page.frames().find(frame => frame.url() === 'https://ibx.key.com/ibxolb/login/client/index.html');

const clickLogin = await frame.click('.login-button-space')

这篇关于尝试使用 puppeteer 单击 iframe 中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆