Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
feat: add `iframe` expansion to `parseWithCheerio` in browsers (#2542)
Jindřich Bär328d08598807
diff --git a/packages/browser-crawler/src/internals/browser-crawler.ts b/packages/browser-crawler/src/internals/browser-crawler.tsindex 6f514599a..716fcb9cb 100644--- a/packages/browser-crawler/src/internals/browser-crawler.ts+++ b/packages/browser-crawler/src/internals/browser-crawler.ts@@ -342,6 +342,7 @@ export abstract class BrowserCrawler<persistCookiesPerSession: ow.optional.boolean,useSessionPool: ow.optional.boolean,proxyConfiguration: ow.optional.object.validate(validators.proxyConfiguration),+ ignoreShadowRoots: ow.optional.boolean,};/**@@ -370,6 +371,7 @@ export abstract class BrowserCrawler<failedRequestHandler,handleFailedRequestFunction,headless,+ ignoreShadowRoots,...basicCrawlerOptions} = options;diff --git a/packages/playwright-crawler/src/internals/utils/playwright-utils.ts b/packages/playwright-crawler/src/internals/utils/playwright-utils.tsindex ac70cfcd0..30b7ea43b 100644--- a/packages/playwright-crawler/src/internals/utils/playwright-utils.ts+++ b/packages/playwright-crawler/src/internals/utils/playwright-utils.ts@@ -600,6 +600,28 @@ export async function saveSnapshot(page: Page, options: SaveSnapshotOptions = {}export async function parseWithCheerio(page: Page, ignoreShadowRoots = false): Promise<CheerioRoot> {ow(page, ow.object.validate(validators.browserPage));+ if (page.frames().length > 1) {+ const frames = await page.$$('iframe');++ await Promise.all(+ frames.map(async (frame) => {+ const iframe = await frame.contentFrame();++ if (iframe) {+ const contents = await iframe.content();++ await frame.evaluate((f, c) => {+ const replacementNode = document.createElement('div');+ replacementNode.innerHTML = c;+ replacementNode.className = 'crawlee-iframe-replacement';++ f.replaceWith(replacementNode);+ }, contents);+ }+ }),+ );+ }+const html = ignoreShadowRoots? null: ((await page.evaluate(`(${expandShadowRoots.toString()})(document)`)) as string);diff --git a/packages/puppeteer-crawler/src/internals/utils/puppeteer_utils.ts b/packages/puppeteer-crawler/src/internals/utils/puppeteer_utils.tsindex 143f02c5a..e3ab78b3b 100644--- a/packages/puppeteer-crawler/src/internals/utils/puppeteer_utils.ts+++ b/packages/puppeteer-crawler/src/internals/utils/puppeteer_utils.ts@@ -191,6 +191,28 @@ export async function injectJQuery(page: Page, options?: { surviveNavigations?:export async function parseWithCheerio(page: Page, ignoreShadowRoots = false): Promise<CheerioRoot> {ow(page, ow.object.validate(validators.browserPage));+ if (page.frames().length > 1) {+ const frames = await page.$$('iframe');++ await Promise.all(+ frames.map(async (frame) => {+ const iframe = await frame.contentFrame();++ if (iframe) {+ const contents = await iframe.content();++ await frame.evaluate((f, c) => {+ const replacementNode = document.createElement('div');+ replacementNode.innerHTML = c;+ replacementNode.className = 'crawlee-iframe-replacement';++ f.replaceWith(replacementNode);+ }, contents);+ }+ }),+ );+ }+const html = ignoreShadowRoots? null: ((await page.evaluate(`(${expandShadowRoots.toString()})(document)`)) as string);diff --git a/test/core/playwright_utils.test.ts b/test/core/playwright_utils.test.tsindex cdaf1e35a..67d6b0942 100644--- a/test/core/playwright_utils.test.ts+++ b/test/core/playwright_utils.test.ts@@ -159,6 +159,24 @@ describe('playwrightUtils', () => {}});+ test('parseWithCheerio() iframe expansion works', async () => {+ const browser = await launchPlaywright(launchContext);++ try {+ const page = await browser.newPage();+ await page.goto(new URL('/special/outside-iframe', serverAddress).toString());++ const $ = await playwrightUtils.parseWithCheerio(page);++ const headings = $('h1')+ .map((i, el) => $(el).text())+ .get();+ expect(headings).toEqual(['Outside iframe', 'In iframe']);+ } finally {+ await browser.close();+ }+ });+describe('blockRequests()', () => {let browser: Browser = null;beforeAll(async () => {diff --git a/test/core/puppeteer_utils.test.ts b/test/core/puppeteer_utils.test.tsindex 674a5e7b6..13be599f1 100644--- a/test/core/puppeteer_utils.test.ts+++ b/test/core/puppeteer_utils.test.ts@@ -160,6 +160,24 @@ describe('puppeteerUtils', () => {}});+ test('parseWithCheerio() iframe expansion works', async () => {+ const browser = await launchPuppeteer(launchContext);++ try {+ const page = await browser.newPage();+ await page.goto(new URL('/special/outside-iframe', serverAddress).toString());++ const $ = await puppeteerUtils.parseWithCheerio(page);++ const headings = $('h1')+ .map((i, el) => $(el).text())+ .get();+ expect(headings).toEqual(['Outside iframe', 'In iframe']);+ } finally {+ await browser.close();+ }+ });+describe('blockRequests()', () => {let browser: Browser = null;beforeAll(async () => {diff --git a/test/shared/_helper.ts b/test/shared/_helper.tsindex 8e77fc577..a797cb256 100644--- a/test/shared/_helper.ts+++ b/test/shared/_helper.ts@@ -172,6 +172,28 @@ console.log('Hello world!');</div></body></html>`,+ outsideIframe: `+ <!DOCTYPE html>+ <html>+ <head>+ <title>Outside iframe</title>+ </head>+ <body>+ <h1>Outside iframe</h1>+ <iframe src="./inside-iframe"></iframe>+ </body>+ </html>`,+ insideIframe: `+ <!DOCTYPE html>+ <html>+ <head>+ <title>In iframe</title>+ </head>+ <body>+ <h1>In iframe</h1>+ <p>Some content from inside of an iframe.</p>+ </body>+ </html>`,};export async function runExampleComServer(): Promise<[Server, number]> {@@ -268,6 +290,14 @@ export async function runExampleComServer(): Promise<[Server, number]> {special.get('/cloudflareBlocking', async (_req, res) => {res.type('html').status(403).send(responseSamples.cloudflareBlocking);});++ special.get('/outside-iframe', (_req, res) => {+ res.type('html').send(responseSamples.outsideIframe);+ });++ special.get('/inside-iframe', (_req, res) => {+ res.type('html').send(responseSamples.insideIframe);+ });})();// "cacheable" site with one page, scripts and stylesheets