Skip to main content

Timeout Configuration

This document explains how to configure timeouts for Browserless REST APIs. Proper timeout configuration is essential for reliable browser automation, especially when dealing with slow-loading pages or complex interactions.

Overview

Browserless REST APIs support timeout configuration through two methods:

  1. Query Parameter Timeout: Global timeout for the entire request
  2. JSON Body Timeout Parameters: Granular timeout control for specific operations

All timeout values are specified in milliseconds.

Query Parameter Timeout

You can set a global timeout for any REST API request using the timeout query parameter. This controls the maximum duration for the entire operation.

For detailed information about query parameter timeouts, see the Launch Parameters documentation.

JSON Body Timeout Parameters

REST APIs support several timeout parameters in the JSON request body for fine-grained control over different operations:

Controls how long to wait for page navigation to complete. This is part of the gotoOptions object which mirrors Puppeteer's GoToOptions interface.

{
"url": "https://example.com/",
"gotoOptions": {
"timeout": 30000,
"waitUntil": "networkidle2"
}
}

Selector Timeouts (waitForSelector.timeout)

Controls how long to wait for a specific element to appear on the page. If the element doesn't appear within the timeout period, the operation will fail (unless bestAttempt is enabled).

{
"url": "https://example.com/",
"waitForSelector": {
"selector": "#main-content",
"timeout": 10000,
"visible": true
}
}

Function Timeouts (waitForFunction.timeout)

Controls how long to wait for a custom JavaScript function to return a truthy value. Useful for waiting on complex conditions or asynchronous operations.

{
"url": "https://example.com/",
"waitForFunction": {
"fn": "() => document.querySelector('#dynamic-content').textContent.includes('loaded')",
"timeout": 15000
}
}

Event Timeouts (waitForEvent.timeout)

Controls how long to wait for a specific DOM event to fire before proceeding with the operation.

{
"url": "https://example.com/",
"waitForEvent": {
"event": "load",
"timeout": 8000
}
}

Fixed Delay Timeout (waitForTimeout)

Waits for a specified number of milliseconds before proceeding. Useful when you need to wait for animations, transitions, or other time-based operations to complete.

{
"url": "https://example.com/",
"waitForTimeout": 3000
}

Combining Multiple Timeout Parameters

You can combine multiple timeout parameters in a single request for comprehensive control over the browser automation process:

{
"url": "https://example.com/",
"gotoOptions": {
"timeout": 30000,
"waitUntil": "networkidle2"
},
"waitForSelector": {
"selector": "#content",
"timeout": 10000,
"visible": true
},
"waitForTimeout": 2000
}

Error Handling with bestAttempt

When using timeout parameters, you can enable the bestAttempt option to make Browserless continue processing even if timeout operations fail:

{
"url": "https://example.com/",
"bestAttempt": true,
"waitForSelector": {
"selector": "#might-not-exist",
"timeout": 5000
}
}

With bestAttempt: true, the request will continue even if the selector timeout fails, allowing you to capture partial results.

Best Practices

  1. Set Realistic Timeouts: Don't set timeouts too low for slow-loading content, but avoid unnecessarily high values that could delay error detection.

  2. Use Appropriate waitUntil Options: Choose the right navigation completion criteria:

    • load: Wait for the load event (fastest)
    • domcontentloaded: Wait for DOM to be ready
    • networkidle0: Wait until no network requests for 500ms
    • networkidle2: Wait until ≤2 network requests for 500ms
  3. Combine Timeouts Strategically: Use navigation timeouts for page loading, selector timeouts for dynamic content, and fixed delays sparingly.

  4. Monitor Total Request Time: Remember that the query parameter timeout applies to the entire request, including all wait operations.

  5. Handle Timeout Errors: Always implement proper error handling for timeout scenarios in your application code.

Supported APIs

These timeout parameters are supported across all major Browserless REST APIs:

For API-specific timeout examples and use cases, refer to the individual API documentation pages.