Conditional
Execute different browser actions based on JavaScript conditions evaluated in the page context.
Execute different browser actions depending on the content of the page, using JavaScript expressions evaluated in the browser context.
Command
"if"
Use conditional logic to make your scraping smarter β only proceed with certain actions if specific content is detected.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
condition |
string | Yes | A JavaScript expression that evaluates to true or false. This is executed in the page context. |
then |
array | Yes | An array of browser actions to execute if the condition returns true. |
or |
array | No | An array of browser actions to execute if the condition returns false. |
You can nest if blocks inside each other for complex decision-making.
Example
In this example, we visit a page and:
- If the body text contains the word "terms", we navigate to a different URL.
- Otherwise, we click a button.
Test it with cURL:
Notes
The if conditional action works across all Scrappey clients, including Python, JavaScript (Node.js), PHP, Java, Go, Ruby, C#, and cURL.
Conditional logic is well-suited for handling unexpected redirects, skipping cookie/consent modals, navigating multi-step flows, and avoiding unnecessary browser actions.