Skip to main content
Wait for elements, JavaScript conditions, and network requests before proceeding with automation.

Methods

page.waitForSelector()

Waits for an element matching the selector to appear in the DOM.
Parameters:
  • selector: CSS selector for the element to wait for
  • options (optional): Wait options
Options:
Returns: Promise<void> Examples:

page.waitForFunction()

Waits until a function returns a truthy value.
Parameters:
  • fn: Function to evaluate repeatedly until it returns truthy
  • options (optional): Wait options
  • args (optional): Arguments to pass to the function
Returns: Promise<void> Examples:

page.waitForRequest()

Waits for a matching HTTP request to occur.
Parameters:
  • matcher: URL string or predicate function to match requests
  • options (optional): Wait options
RequestMatchFunction:
Request: the same object the xhr event delivers — request metadata and the response, when one has arrived.
Returns: Promise<Request> - The matched request Examples:
Reading the response body: response is optional — a request can be reported before its response is in. To get the body, require it in the matcher so the promise resolves on the completed request rather than the in-flight one:
There is no need to collect xhr events into an array to get at response bodies — the awaited result of waitForRequest() already carries the response.

Complete examples

Wait for page load

Wait for dynamic content

Wait for API response

Sequential waits

Timeout handling

Wait for multiple conditions

Wait for text content

Poll for changes

Wait for network idle

Best practices

Wait for elements before clicking or typing:
Set timeouts based on expected load times:
Always catch timeout errors gracefully:
Wait for several conditions to ensure page is ready:

Navigation

Navigate and track page loads

Events

Monitor page events

Interaction

Interact after waiting

Page overview

Back to Page API overview