# Conditional

Source: https://docs.scrappey.com/docs/conditional

> 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.

```json
{
  "cmd": "request.get",
  "url": "https://www.arena-top100.com/index.php?a=in&u=sergey1234",
  "browserActions": [
    {
      "type": "if",
      "condition": "document.querySelector('body').innerText.toLowerCase().includes('terms')",
      "then": [
        {
          "type": "if",
          "condition": "document.querySelector('body').innerText.toLowerCase().includes('terms')",
          "then": [
            {
              "type": "goto",
              "url": "https://www.arena-top100.com/index.php?a=in&u=test1"
            }
          ]
        }
      ],
      "or": [
        {
          "type": "click",
          "cssSelector": "#continueButton"
        }
      ]
    }
  ]
}
```

Test it with cURL:

```bash
curl -X POST "https://publisher.scrappey.com/api/v1?key=YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "cmd": "request.get",
    "url": "https://www.arena-top100.com/index.php?a=in&u=sergey1234",
    "browserActions": [ ... ]
  }'
```

## 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.
