【问题标题】:How to get iframe content or link in puppeteer?如何在 puppeteer 中获取 iframe 内容或链接?
【发布时间】:2021-08-22 18:42:41
【问题描述】:
首先,对不起我的英语。我不是母语人士,所以。我正在开发一个关于 React Native 的新闻应用程序。为此,我正在做一个关于 puppeteer 的新闻报道。问题出在 iframe 中。我无法得到它。我试图评估页面并使用 querySelector。它没有找到它。
我该怎么办?我只想获取 iframe 内容或链接以将其显示在 .
我要抓取的页面 - link
在那里,我需要一个电报 iframe。我已经成功抓取了它的标题,内容,文章照片等。所以,我只需要iframe抓取的代码。
【问题讨论】:
标签:
javascript
html
node.js
iframe
puppeteer
【解决方案1】:
这是解决方案:
await page.goto(`https://sharij.net/lukashenko-my-ne-mozhem-stat-ukrainoj-i-voevat-protiv-rossii`, { waitUntil: 'networkidle0', timeout: 0 })
await page.waitForSelector("iframe[id^=telegram]")
await page.evaluate(() => {
const frameDocument = document.querySelector("iframe[id^=telegram]").contentWindow.document // this is the solution
const videoMessage = frameDocument.querySelector("div.tgme_widget_message") // just for checking that we can access elements
console.log(videoMessage.innerText)
// in same way you can get anything now from this iframe :)
})