这是我第一次使用 puppeteer 进行一些轻型自动化。我在尝试单击位于 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
返回。
我可以访问 iframe 的选择器是 src
和 title
。如您所见,我正在使用 src
。
我搜索了又搜索如何执行此操作,但似乎没有一个示例适合我。
我被困住了,所以如果有人有任何建议、解决方案或方向,我将不胜感激。
谢谢,
乔
最佳答案
如果您从 *.key.com
或 *.keybank.com
旁边的 URL/站点访问 iframe,则无法打开 iframe,因为CSP 指令。
打开 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".
如果您没有看到上述任何消息,您可以这样尝试
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')
关于javascript - 尝试使用 puppeteer 单击 iframe 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59431296/