Introduction
Our API allows you to interact dynamically with webpages through JavaScript scenarios. These scenarios enable you to execute various actions such as clicking buttons, filling forms, and scrolling, prior to scraping the desired HTML.
Basic Usage
To utilize this feature, you need to include a js_scenario
parameter in your API request. The parameter is a JSON object consisting of an array of instructions.
Example
Clicking a button:
{
"instructions": [
{"click": "#buttonId"}
]
}
This example interacts with the webpage, clicks on the element with ID buttonId
, and returns the updated HTML.
Selectors
You can use both CSS and XPath selectors. Selectors beginning with /
are treated as XPath; others are treated as CSS.
Strict Mode
By default, the scenario operates in strict mode, which means execution halts on any error. You can disable this behavior by setting "strict": false
.
{
"strict": false,
"instructions": [
{"click": "#buttonId"}
]
}
Supported Instructions
Click
Clicks on an element specified by a CSS/XPath selector.
{"click": "#buttonId"}
Wait
Pauses for a specified duration (in milliseconds).
{"wait": 2000}
Wait for an Element
Waits for an element to appear on the page.
{"wait_for": ".elementClass"}
Wait and Click
Combines waiting for an element and clicking on it.
{"wait_for": ".elementClass"}
{"click": ".elementClass"}
Scroll
Scrolls horizontally or vertically.
- Horizontal:
{"scroll_x": 500}
- Vertical:
{"scroll_y": 500}
Fill Input
Fills an input field with a value.
{"fill": ["#inputField", "value"]}
Custom JavaScript
Executes a custom JavaScript code snippet.
{"evaluate": "console.log('Hello World')"}
Example Scenarios
Complex Scenario
Wait for a button to appear, click it, then scroll twice with delays.
{
"instructions": [
{"wait_for": "#slowButton"},
{"click": "#slowButton"},
{"scroll_x": 1000},
{"wait": 1000},
{"scroll_y": 1000}
]
}