playwrightUtils
Index
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*
.
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 theDOMContentLoaded
event is fired.'load'
- consider operation to be finished when theload
event is fired.'networkidle'
- consider operation to be finished when there are no network connections for at least500
ms.
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
Parameters
optionaloptions: BlockRequestsOptions
Returns Promise<void>
injectFile
Parameters
filePath: string
optionaloptions: InjectFileOptions
Returns Promise<unknown>
injectJQuery
Returns Promise<unknown>
parseWithCheerio
Returns Promise<CheerioAPI>
Functions
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
extraUrlPatterns
option, which will keep blocking the default patterns, as well as add your custom ones. If you would like to block only specific patterns, use theurlPatterns
option, 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
Page
object.optionaloptions: BlockRequestsOptions = {}
Returns Promise<void>
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
Page
object.request: Request<Dictionary<any>>
optionalgotoOptions: DirectNavigationOptions = {}
Custom options for
page.goto()
.
Returns Promise<Response | null>
injectFile
Injects a JavaScript file into a Playwright page. Unlike Playwright's
addScriptTag
function, 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
Page
object.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.
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
Page
object.
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
Page
object.
Returns Promise<CheerioRoot>
registerUtilsToContext
Parameters
context: PlaywrightCrawlingContext<Dictionary<any>>
Returns void
A namespace that contains various utilities for Playwright - the headless Chrome Node API.
Example usage: