Skip to main content
Execute JavaScript in the browser context and inject user scripts that run on every page load.

Methods

page.evaluate()

Executes JavaScript in the page context and returns the result.
Parameters:
  • func: Function or string of JavaScript code to execute
  • params (optional): Parameters to pass to the function
Returns: Promise<unknown> - Result of the JavaScript execution Examples:
evaluate() runs in the page context, so it has access to the DOM, window, document, and any JavaScript libraries loaded on the page.

page.addUserScript()

Adds a user script that runs on every page load.
Parameters:
  • script: JavaScript code to execute on every page load
Returns: Promise<void> Example:

Complete examples

Extract data from page

Check page state

Manipulate DOM

Call page functions

Scroll and interact

Inject libraries

Monitor page activity

Custom form validation

Best practices

evaluate() can only return serializable values (no functions, DOM nodes, etc.):
Errors in evaluate() will be thrown in your code:
Pass values as parameters rather than using string concatenation:
User scripts run before page scripts, so you can set up monitoring or modify behavior:

Waiting

Wait for JavaScript conditions

Interaction

Interact with page elements

Events

Monitor page events

Page overview

Back to Page API overview