简体   繁体   English

尝试使用 puppeteer 单击 iframe 中的按钮

[英]Trying to click a button within an iframe with puppeteer

This is my first time using puppeteer for some light automation.这是我第一次使用 puppeteer 进行一些轻型自动化。 I am running into an issues trying to click a button that is located within an iframe.我在尝试单击 iframe 中的按钮时遇到问题。

Here is my code:这是我的代码:

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

The getFrame() errors because the frameElement is return as a JSHandle@node . getFrame()错误,因为 frameElement 作为JSHandle@node返回。

The selectors I have access to for the iframe are src and title .我可以访问 iframe 的选择器是srctitle As you can see I am using src .如您所见,我正在使用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.我被卡住了,所以如果有人有任何建议、解决方案或方向,我将不胜感激。

Thank you,谢谢,

Joe

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.如果您从*.key.com*.keybank.com旁边的 URL / 站点访问 iframe,则由于 CSP 指令,您无法打开 iframe。

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.打开 chrome 开发者控制台,如果您能看到此消息,那是因为 CSP 指令不允许您访问 iframe 站点 URL。

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".拒绝在框架中显示“ https://ibx.key.com/ibxolb/login/client/index.html ”,因为祖先违反了以下内容安全策略指令:“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')

I was able to solve my issue.我能够解决我的问题。

First I got all of the frames listed on the page like so:首先,我得到了页面上列出的所有框架,如下所示:

var frames = (await page.frames());

Then I ended up looping through the frames looking for a specific title .然后我最终在框架中循环寻找特定的标题

exports.getFrame = async function(frames, ssoFrameTitle) {
let selectedFrame;
for(let i = 0 ; i < frames.length ; ++i) {
    let frameTitle = await frames[i].title();
    if(frameTitle === ssoFrameTitle) {
        selectedFrame = frames[i];
        break;
    }
}
return selectedFrame;};

Once I locate the frame object I returned it from the function to locate and click the button.找到框架对象后,我从函数中返回它以定位并单击按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM