The evaluate_js parameter allows you to run JavaScript code in the page context before rendering occurs.

Overview

When rendering a webpage with dynamic content, you may need to manipulate the DOM, trigger actions, or wait for content to load. The evaluate_js parameter lets you execute custom JavaScript code during the rendering process.

Parameter details

evaluate_js
object

Allows you to run JavaScript code in the page context before rendering occurs

Usage

{
  "parts": "https://example.com",
  "evaluate_js": {
    "expression": "document.querySelector('.load-more-button').click()"
  }
}

Behavior

The JavaScript expression is evaluated in the page context before rendering begins. If the expression returns a Promise, the rendering process will wait for the Promise to resolve before proceeding.

Use cases

This parameter is particularly useful for:

  1. Loading dynamic content: Trigger actions that load additional content before capturing the page.

  2. DOM manipulation: Modify page elements, styles, or attributes before rendering.

  3. Waiting for conditions: Ensure specific elements or data are available before rendering.

Examples

Clicking a Button

{
  "parts": "https://example.com",
  "evaluate_js": {
    "expression": "document.querySelector('#load-more').click()"
  }
}

Waiting for content to load

{
  "parts": "https://example.com",
  "evaluate_js": {
    "expression": "new Promise(resolve => { 
      const checkContent = () => {
        if (document.querySelector('.dynamic-content')) {
          resolve();
        } else {
          setTimeout(checkContent, 100);
        }
      };
      checkContent();
    })"
  }
}

Modifying page content

{
  "parts": "https://example.com",
  "evaluate_js": {
    "expression": "document.querySelectorAll('.advertisement').forEach(ad => ad.remove())"
  }
}

Scrolling the page

{
  "parts": "https://example.com",
  "evaluate_js": {
    "expression": "window.scrollTo(0, document.body.scrollHeight)"
  }
}