playwrightUtils
Index
Interfaces
Type Aliases
Functions
Interfaces
BlockRequestsOptions
optionalextraUrlPatterns
If you just want to append to the default blocked patterns, use this property.
optionalurlPatterns
The patterns of URLs to block from being loaded by the browser.
Only * can be used as a wildcard. It is also automatically added to the beginning
and end of the pattern. This limitation is enforced by the DevTools protocol.
.png is the same as *.png*.
CompiledScriptParams
page
request
DirectNavigationOptions
optionalreferer
Referer header value. If provided it will take preference over the referer header value set by page.setExtraHTTPHeaders(headers).
optionaltimeout
Maximum operation time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. The
default value can be changed by using the browserContext.setDefaultNavigationTimeout(timeout),
browserContext.setDefaultTimeout(timeout), page.setDefaultNavigationTimeout(timeout) or
page.setDefaultTimeout(timeout) methods.
optionalwaitUntil
When to consider operation succeeded, defaults to load. Events can be either:
'domcontentloaded'- consider operation to be finished when theDOMContentLoadedevent is fired.'load'- consider operation to be finished when theloadevent is fired.'networkidle'- consider operation to be finished when there are no network connections for at least500ms.
HandleCloudflareChallengeOptions
optionalclickCallback
Allows overriding the checkbox clicking. The boundingBox gives you approximate coordinates of the checkbox, use this if you need to adjust the click position.
Type declaration
Parameters
page: Page
boundingBox: { x: number; y: number }
x: number
y: number
Returns Promise<void>
optionalclickPositionCallback
Allows overriding how the checkbox click position is calculated.
Type declaration
Parameters
page: Page
Returns Promise<null | { x: number; y: number }>
optionalisBlockedCallback
Allows overriding the detection of Cloudflare "blocked page".
Type declaration
Parameters
page: Page
Returns Promise<boolean>
optionalisChallengeCallback
Allows overriding the detection of Cloudflare "challenge page".
Type declaration
Parameters
page: Page
Returns Promise<boolean>
optionalpreChallengeSleepSecs
Optional delay (in seconds) before the first click attempt on the challenge checkbox. Defaults to 1s.
optionalsleepSecs
How long should we wait after the challenge is completed for the final page to load.
optionalverbose
Logging defaults to the debug level, use this flag to log to info level instead.
InfiniteScrollOptions
optionalbuttonSelector
Optionally checks and clicks a button if it appears while scrolling. This is required on some websites for the scroll to work.
optionalmaxScrollHeight
How many pixels to scroll down. If 0, will scroll until bottom of page.
optionalscrollDownAndUp
If true, it will scroll up a bit after each scroll down. This is required on some websites for the scroll to work.
optionalstopScrollCallback
This function is called after every scroll and stops the scrolling process if it returns true. The function can be async.
Type declaration
Returns unknown
optionaltimeoutSecs
How many seconds to scroll for. If 0, will scroll until bottom of page.
optionalwaitForSecs
How many seconds to wait for no new content to load before exit.
InjectFileOptions
optionalsurviveNavigations
Enables the injected script to survive page navigations and reloads without need to be re-injected manually. This does not mean, however, that internal state will be preserved. Just that it will be automatically re-injected on each navigation before any other scripts get the chance to execute.
PlaywrightContextUtils
blockRequests
Forces the Playwright browser tab to block loading URLs that match a provided pattern. This is useful to speed up crawling of websites, since it reduces the amount of data that needs to be downloaded from the web, but it may break some websites or unexpectedly prevent loading of resources.
By default, the function will block all URLs including the following patterns:
[".css", ".jpg", ".jpeg", ".png", ".svg", ".gif", ".woff", ".pdf", ".zip"]If you want to extend this list further, use the
extraUrlPatternsoption, which will keep blocking the default patterns, as well as add your custom ones. If you would like to block only specific patterns, use theurlPatternsoption, which will override the defaults and block only URLs with your custom patterns.This function does not use Playwright's request interception and therefore does not interfere with browser cache. It's also faster than blocking requests using interception, because the blocking happens directly in the browser without the round-trip to Node.js, but it does not provide the extra benefits of request interception.
The function will never block main document loads and their respective redirects.
Example usage
preNavigationHooks: [async ({ blockRequests }) => {// Block all requests to URLs that include `adsbygoogle.js` and also all defaults.await blockRequests({extraUrlPatterns: ['adsbygoogle.js'],});},],Parameters
optionaloptions: BlockRequestsOptions
Returns Promise<void>
compileScript
Compiles a Playwright script into an async function that may be executed at any time by providing it with the following object:
{page: Page,request: Request,}Where
pageis a PlaywrightPageandrequestis a Request.The function is compiled by using the
scriptStringparameter as the function's body, so any limitations to function bodies apply. Return value of the compiled function is the return value of the function body = thescriptStringparameter.As a security measure, no globals such as
processorrequireare accessible from within the function body. Note that the function does not provide a safe sandbox and even though globals are not easily accessible, malicious code may still execute in the main process via prototype manipulation. Therefore you should only use this function to execute sanitized or safe code.Custom context may also be provided using the
contextparameter. To improve security, make sure to only pass the really necessary objects to the context. Preferably making secured copies beforehand.Parameters
scriptString: string
optionalctx: Dictionary
Returns CompiledScriptFunction
enqueueLinksByClickingElements
The function finds elements matching a specific CSS selector in a Playwright page, clicks all those elements using a mouse move and a left mouse button click and intercepts all the navigation requests that are subsequently produced by the page. The intercepted requests, including their methods, headers and payloads are then enqueued to a provided RequestQueue. This is useful to crawl JavaScript heavy pages where links are not available in
hrefelements, but rather navigations are triggered in click handlers. If you're looking to find URLs inhrefattributes of the page, see enqueueLinks.Optionally, the function allows you to filter the target links' URLs using an array of glob or regexp patterns.
IMPORTANT: To be able to do this, this function uses various mutations on the page, such as changing the Z-index of elements being clicked and their visibility. Therefore, it is recommended to only use this function as the last operation in the page.
USING HEADFUL BROWSER: When using a headful browser, this function will only be able to click elements in the focused tab, effectively limiting concurrency to 1. In headless mode, full concurrency can be achieved.
PERFORMANCE: Clicking elements with a mouse and intercepting requests is not a low level operation that takes nanoseconds. It's not very CPU intensive, but it takes time. We strongly recommend limiting the scope of the clicking as much as possible by using a specific selector that targets only the elements that you assume or know will produce a navigation. You can certainly click everything by using the
*selector, but be prepared to wait minutes to get results on a large and complex page.Example usage
async requestHandler({ enqueueLinksByClickingElements }) {await enqueueLinksByClickingElements({selector: 'a.product-detail',include: ['https://www.example.com/handbags/**','https://www.example.com/purses/**',],});});Parameters
options: Omit<EnqueueLinksByClickingElementsOptions, requestManager | page>
Returns Promise<BatchAddRequestsResult>
Promise that resolves to BatchAddRequestsResult object.
handleCloudflareChallenge
This helper tries to solve the Cloudflare challenge automatically by clicking on the checkbox. It will try to detect the Cloudflare page, click on the checkbox, and wait for 10 seconds (configurable via
sleepSecsoption) for the page to load. Use this in thepostNavigationHooks, a failures will result in a SessionError which will be automatically retried, so only successful requests will get into therequestHandler.On a successfully solved challenge the page is reloaded and the new Response is returned, which can be returned from the hook to update the crawling context's
response. For the common case, prefer the pre-wrapped handleCloudflareChallengeHook hook.Example usage
postNavigationHooks: [async (context) => ({ response: await context.handleCloudflareChallenge() }),],Parameters
optionaloptions: HandleCloudflareChallengeOptions
Returns Promise<undefined | Response>
infiniteScroll
Scrolls to the bottom of a page, or until it times out. Loads dynamic content when it hits the bottom of a page, and then continues scrolling.
Parameters
optionaloptions: InfiniteScrollOptions
Returns Promise<void>
injectFile
Injects a JavaScript file into current
page. Unlike Playwright'saddScriptTagfunction, this function works on pages with arbitrary Cross-Origin Resource Sharing (CORS) policies.File contents are cached for up to 10 files to limit file system access.
Parameters
filePath: string
optionaloptions: InjectFileOptions
Returns Promise<unknown>
injectJQuery
Injects the jQuery library into current
page. jQuery is often useful for various web scraping and crawling tasks. For example, it can help extract text from HTML elements using CSS selectors.Beware that the injected jQuery object will be set to the
window.$variable and thus it might cause conflicts with other libraries included by the page that use the same variable name (e.g. another version of jQuery). This can affect functionality of page's scripts.The injected jQuery will survive page navigations and reloads.
Example usage:
async requestHandler({ page, injectJQuery }) {await injectJQuery();const title = await page.evaluate(() => {return $('head title').text();});});Note that
injectJQuery()does not affect the Playwrightpage.$()function in any way.Returns Promise<unknown>
listDownloads
Returns the list of Download objects collected during the current page navigation and request handler.
Useful for accessing files that the page downloads automatically. For most use cases, prefer re-enqueueing the URL to FileDownload. Use this only when direct access to the Playwright
Downloadobject is required.Example usage
requestHandler: async ({ listDownloads }) => {for (const download of await listDownloads()) {try {const stream = await download.createReadStream();// stream to storage...} catch {// download failed or was cancelled}}},Returns Promise<Download[]>
parseWithCheerio
Returns Cheerio handle for
page.content(), allowing to work with the data same way as with CheerioCrawler. When provided with theselectorargument, it waits for it to be available first.Example usage:
async requestHandler({ parseWithCheerio }) {const $ = await parseWithCheerio();const title = $('title').text();});Parameters
optionalselector: string
optionaltimeoutMs: number
Returns Promise<CheerioAPI>
saveSnapshot
Saves a full screenshot and HTML of the current page into a Key-Value store.
Parameters
optionaloptions: SaveSnapshotOptions
Returns Promise<void>
waitForSelector
Wait for an element matching the selector to appear. Timeout defaults to 5s.
Example usage:
async requestHandler({ waitForSelector, parseWithCheerio }) {await waitForSelector('article h1');const $ = await parseWithCheerio();const title = $('title').text();});Parameters
selector: string
optionaltimeoutMs: number
Returns Promise<void>
SaveSnapshotOptions
optionalconfiguration
Configuration of the crawler that will be used to save the snapshot.
optionalkey
Key under which the screenshot and HTML will be saved. .jpg will be appended for screenshot and .html for HTML.
optionalkeyValueStoreName
Name or id of the Key-Value store where snapshot is saved. By default it is saved to default Key-Value store.
optionalsaveHtml
If true, it will save a full HTML of the current page as a record with key appended by .html.
optionalsaveScreenshot
If true, it will save a full screenshot of the current page as a record with key appended by .jpg.
optionalscreenshotQuality
The quality of the image, between 0-100. Higher quality images have bigger size and require more storage.
Type Aliases
CompiledScriptFunction
Type declaration
Parameters
params: CompiledScriptParams
Returns Promise<unknown>
Functions
blockRequests
This is a Chromium-only feature.
Using this option with Firefox and WebKit browsers doesn't have any effect. To set up request blocking for these browsers, use
page.route()instead.Forces the Playwright browser tab to block loading URLs that match a provided pattern. This is useful to speed up crawling of websites, since it reduces the amount of data that needs to be downloaded from the web, but it may break some websites or unexpectedly prevent loading of resources.
By default, the function will block all URLs including the following patterns:
[".css", ".jpg", ".jpeg", ".png", ".svg", ".gif", ".woff", ".pdf", ".zip"]If you want to extend this list further, use the
extraUrlPatternsoption, which will keep blocking the default patterns, as well as add your custom ones. If you would like to block only specific patterns, use theurlPatternsoption, which will override the defaults and block only URLs with your custom patterns.This function does not use Playwright's request interception and therefore does not interfere with browser cache. It's also faster than blocking requests using interception, because the blocking happens directly in the browser without the round-trip to Node.js, but it does not provide the extra benefits of request interception.
The function will never block main document loads and their respective redirects.
Example usage
import { launchPlaywright, playwrightUtils } from 'crawlee';const browser = await launchPlaywright();const page = await browser.newPage();// Block all requests to URLs that include `adsbygoogle.js` and also all defaults.await playwrightUtils.blockRequests(page, {extraUrlPatterns: ['adsbygoogle.js'],});await page.goto('https://cnn.com');Parameters
page: Page
Playwright
Pageobject.optionaloptions: BlockRequestsOptions = {}
Returns Promise<void>
compileScript
Compiles a Playwright script into an async function that may be executed at any time by providing it with the following object:
{page: Page,request: Request,}Where
pageis a PlaywrightPageandrequestis a Request.The function is compiled by using the
scriptStringparameter as the function's body, so any limitations to function bodies apply. Return value of the compiled function is the return value of the function body = thescriptStringparameter.As a security measure, no globals such as
processorrequireare accessible from within the function body. Note that the function does not provide a safe sandbox and even though globals are not easily accessible, malicious code may still execute in the main process via prototype manipulation. Therefore you should only use this function to execute sanitized or safe code.Custom context may also be provided using the
contextparameter. To improve security, make sure to only pass the really necessary objects to the context. Preferably making secured copies beforehand.Parameters
scriptString: string
context: Dictionary = ...
Returns CompiledScriptFunction
enqueueLinksByClickingElements
The function finds elements matching a specific CSS selector in a Playwright page, clicks all those elements using a mouse move and a left mouse button click and intercepts all the navigation requests that are subsequently produced by the page. The intercepted requests, including their methods, headers and payloads are then enqueued to a provided RequestQueue. This is useful to crawl JavaScript heavy pages where links are not available in
hrefelements, but rather navigations are triggered in click handlers. If you're looking to find URLs inhrefattributes of the page, see enqueueLinks.Optionally, the function allows you to filter the target links' URLs using an array of glob or regexp patterns.
IMPORTANT: To be able to do this, this function uses various mutations on the page, such as changing the Z-index of elements being clicked and their visibility. Therefore, it is recommended to only use this function as the last operation in the page.
USING HEADFUL BROWSER: When using a headful browser, this function will only be able to click elements in the focused tab, effectively limiting concurrency to 1. In headless mode, full concurrency can be achieved.
PERFORMANCE: Clicking elements with a mouse and intercepting requests is not a low level operation that takes nanoseconds. It's not very CPU intensive, but it takes time. We strongly recommend limiting the scope of the clicking as much as possible by using a specific selector that targets only the elements that you assume or know will produce a navigation. You can certainly click everything by using the
*selector, but be prepared to wait minutes to get results on a large and complex page.Example usage
await playwrightUtils.enqueueLinksByClickingElements({page,requestManager,selector: 'a.product-detail',include: ['https://www.example.com/handbags/*','https://www.example.com/purses/*',],});Parameters
Returns Promise<BatchAddRequestsResult>
Promise that resolves to BatchAddRequestsResult object.
gotoExtended
Extended version of Playwright's
page.goto()allowing to perform requests with HTTP method other than GET, with custom headers and POST payload. URL, method, headers and payload are taken from request parameter that must be an instance of Request class.NOTE: In recent versions of Playwright using requests other than GET, overriding headers and adding payloads disables browser cache which degrades performance.
Parameters
page: Page
Playwright
Pageobject.request: CrawleeRequest<Dictionary>
optionalgotoOptions: DirectNavigationOptions = {}
Custom options for
page.goto().
Returns Promise<Response | null>
infiniteScroll
Scrolls to the bottom of a page, or until it times out. Loads dynamic content when it hits the bottom of a page, and then continues scrolling.
Parameters
page: Page
Playwright
Pageobject.optionaloptions: InfiniteScrollOptions = {}
Returns Promise<void>
injectFile
Injects a JavaScript file into a Playwright page. Unlike Playwright's
addScriptTagfunction, this function works on pages with arbitrary Cross-Origin Resource Sharing (CORS) policies.File contents are cached for up to 10 files to limit file system access.
Parameters
page: Page
Playwright
Pageobject.filePath: string
File path
optionaloptions: InjectFileOptions = {}
Returns Promise<unknown>
injectJQuery
Injects the jQuery library into a Playwright page. jQuery is often useful for various web scraping and crawling tasks. For example, it can help extract text from HTML elements using CSS selectors.
Beware that the injected jQuery object will be set to the
window.$variable and thus it might cause conflicts with other libraries included by the page that use the same variable name (e.g. another version of jQuery). This can affect functionality of page's scripts.The injected jQuery will survive page navigations and reloads by default.
Example usage:
await playwrightUtils.injectJQuery(page);const title = await page.evaluate(() => {return $('head title').text();});Note that
injectJQuery()does not affect the Playwrightpage.$()function in any way.Parameters
page: Page
Playwright
Pageobject.optionaloptions: { surviveNavigations?: boolean }
optionalsurviveNavigations: boolean
Opt-out option to disable the JQuery reinjection after navigation.
Returns Promise<unknown>
parseWithCheerio
Returns Cheerio handle for
page.content(), allowing to work with the data same way as with CheerioCrawler.Example usage:
const $ = await playwrightUtils.parseWithCheerio(page);const title = $('title').text();Parameters
page: Page
Playwright
Pageobject.ignoreShadowRoots: boolean = false
ignoreIframes: boolean = false
Returns Promise<CheerioAPI>
saveSnapshot
Saves a full screenshot and HTML of the current page into a Key-Value store.
Parameters
page: Page
Playwright
Pageobject.optionaloptions: SaveSnapshotOptions = {}
Returns Promise<void>
A namespace that contains various utilities for Playwright - the headless Chrome Node API.
Example usage: