While loop

guides1 section

🔁 While Loop (Browser Action)

Repeat a set of browser actions until a specific condition is no longer true.


🧠 Command

"while"

Use this command to perform looping logic within the browser context. It’s useful for tasks like handling CAPTCHAs, paginated scraping, or waiting for content to disappear/appear.


🔧 Parameters

Parameter Required Description
condition ✅ Yes A JavaScript expression that returns true or false. The loop continues as long as it evaluates to true.
then ✅ Yes Array of browser actions to execute on each iteration while the condition is true.
maxAttempts ❌ Optional Max number of loop iterations to prevent infinite loops.

🧪 Example Request (cURL)

curl -X POST "https://publisher.scrappey.com/api/v1?key=API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "cmd": "request.get",
    "url": "https://example.com",
    "browserActions": [
      {
        "type": "while",
        "condition": "document.querySelector('body').innerText.toLowerCase().includes('terms')",
        "then": [
          {
            "type": "goto",
            "url": "https://example.com/alternate-page"
          },
          {
            "type": "solve_captcha",
            "captcha": "turnstile"
          }
        ]
      }
    ]
  }'

🧠 Use Case Ideas

  • Looping through paginated content until no "Next" button appears
  • Retrying CAPTCHA solving until successful
  • Waiting for an element to disappear before proceeding

✅ Notes

  • You can combine this with other browser actions like click, type, goto, wait, or even another if or while block.
  • Always consider setting a maxAttempts to avoid infinite loops and excessive resource usage.
Was this page helpful?
Last updated: May 28, 2025
📚 AI Guide