Methods
page.evaluate()
Executes JavaScript in the page context and returns the result.
func: Function or string of JavaScript code to executeparams(optional): Parameters to pass to the function
Promise<unknown> - Result of the JavaScript execution
Examples:
page.addUserScript()
Adds a user script that runs on every page load.
script: JavaScript code to execute on every page load
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
Serialize return values
Serialize return values
evaluate() can only return serializable values (no functions, DOM nodes, etc.):Handle errors in evaluate
Handle errors in evaluate
Errors in
evaluate() will be thrown in your code:Use parameters for dynamic values
Use parameters for dynamic values
Pass values as parameters rather than using string concatenation:
User scripts run early
User scripts run early
User scripts run before page scripts, so you can set up monitoring or modify behavior:
Related
Waiting
Wait for JavaScript conditions
Interaction
Interact with page elements
Events
Monitor page events
Page overview
Back to Page API overview