Skip to main content
Version: 2.3

utils.playwright

A namespace that contains various utilities for Playwright - the headless Chrome Node API.

Example usage:

const Apify = require('apify');
const { playwright } = Apify.utils;

// Navigate to https://www.example.com in Playwright with a POST request
const browser = await Apify.launchPlaywright();
const page = await browser.newPage();
await playwright.gotoExtended(page, {
url: 'https://example.com,
method: 'POST',
});

playwright.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 Apify.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:

Returns:

Promise<(Response|null)>


playwright.injectFile(page, filePath, [options])

Injects a JavaScript file into a Playright 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
  • [options]: object
    • [surviveNavigations]: boolean - 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.

Returns:

Promise<*>


playwright.injectJQuery(page)

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 Apify.utils.playwright.injectJQuery(page);
const title = await page.evaluate(() => {
return $('head title').text();
});

Note that injectJQuery() does not affect the Playwright page.$() function in any way.

Parameters:

  • page: Page - Playwright Page object.

Returns:

Promise<*>