Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

javascript - Trying to click a button within an iframe with puppeteer

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.

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.

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.

Thank you,

Joe

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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')


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...