Skip to content

Browser steps

Some data only appears after you interact with the page: a search form, a “Load more” button, a cookie banner, a tab, a date picker. steps tells the real Chrome of /v2/scrape-js (and /scrape-js) what to do on the page before ScrapeNinja returns the HTML, the screenshot and the extractor result.

"steps": [
{ "type": "change", "selector": "input[name=q]", "value": "mechanical keyboard" },
{ "type": "click", "selector": "button[type=submit]", "waitForNavigation": true },
{ "type": "waitForElement", "selector": ".search-results .item" }
]
  1. The page loads, then ScrapeNinja waits for waitForSelector and postWaitTime, if you set them.
  2. The steps run one by one, in order. Each step waits until its element is visible, scrolls it into view and acts on it.
  3. If a step fails, the whole attempt fails and is retried from another proxy (up to retryNum), like any other error.
  4. After the last step: catchAjaxHeadersUrlMask, the screenshot, the page HTML and your extractor.
type What it does Fields
click Clicks the element. selector or selectors, optional waitForNavigation, waitForXhrRequest
change Types into an input or textarea, or selects an option of a <select>. selector or selectors, value, optional waitForNavigation, waitForXhrRequest
waitForElement Waits until the element is visible. Use it after a click that loads content without a navigation. selector or selectors

All fields:

Field Type Description
type string click, change or waitForElement.
selector string Element to act on.
selectors string[] Alternatives instead of one selector: the first one that matches a visible element wins. Useful when a site renders different markup for different visitors.
value string change only: text to type, or the value of the <option> to select.
waitForNavigation boolean Wait for the page navigation the step causes (a form submit, a link).
waitForXhrRequest string Part of the URL of an XHR the step triggers. Its response is returned in info.log (see below).

A selector is a regular CSS selector, plus the query prefixes of Puppeteer:

Selector Matches
button.load-more CSS, as in document.querySelector
text/Load more the element whose text contains “Load more”
aria/Search the element with the accessible name “Search” (label, aria-label, button text)
xpath///button[contains(., 'More')] an XPath expression
pierce/.price CSS that also looks inside shadow DOM

Text and ARIA selectors survive redesigns better than generated class names like .css-1x2y3z.

change behaves like a person:

  • on a <select>, the option whose value equals value is selected;
  • on text-like inputs (text, search, email, url, tel, number, password, textarea), the text is typed key by key, so autocomplete and validation scripts react as they do for a human; if the field already starts with the same text, only the rest is typed;
  • on other elements, value is set directly and input and change events are fired.

Many sites load the data with a background request after a click. Set waitForXhrRequest to a part of its URL, and ScrapeNinja waits for that request and returns its response, so you often don’t need to parse HTML at all:

"steps": [
{ "type": "click", "selector": "text/Show prices", "waitForXhrRequest": "/api/prices" }
]

The response arrives in info.log:

"info": {
"log": [
{
"type": "xhr",
"stepIdx": 1,
"data": { "url": "https://shop.example/api/prices?id=42", "method": "GET", "status": 200, "body": "{\"price\": 321.4}" }
}
]
}

To catch an XHR the page makes on its own while loading (no step needed), use the request-level catchAjaxHeadersUrlMask instead: its response arrives in info.catchedAjax.

You don’t have to write steps by hand. Chrome can record them:

  1. Open the target page in Chrome, then DevTools → Recorder (⋮ → More tools → Recorder).
  2. Click Create a new recording, do what you need on the page, stop the recording.
  3. Export it as JSON and copy the steps array into your ScrapeNinja request.
  4. Keep only the click, change and waitForElement steps. Remove setViewport, navigate, keyDown/keyUp and other types: ScrapeNinja loads the URL itself, and other types are rejected with a validation error.

Recorder steps carry selectors as nested arrays like [["aria/Search"], ["#q"]]; ScrapeNinja accepts them as they are and uses the first visible match.

Terminal window
curl https://scrapeninja.apiroad.net/v2/scrape-js \
-H "Content-Type: application/json" \
-H "X-Apiroad-Key: YOUR_KEY" \
-d '{
"url": "https://shop.example/",
"geo": "us",
"steps": [
{ "type": "click", "selectors": ["#accept-cookies", "text/Accept"] },
{ "type": "change", "selector": "input[type=search]", "value": "mechanical keyboard" },
{ "type": "click", "selector": "button[type=submit]", "waitForNavigation": true },
{ "type": "waitForElement", "selector": ".product-card" }
],
"extractorPreset": "markdown_content",
"outputBody": false
}'
  • Up to 50 steps per request.
  • Each step has 11 seconds to find a visible element and act. The request timeout still applies to the whole attempt, so give long step lists a higher timeout.
  • If an element exists but stays hidden for 3 seconds (for example a link inside a collapsed menu), a click falls back to a DOM click on it.
  • A step that can’t find its element fails the attempt with SELECTOR_TIMEOUT and a message naming the step and the selectors it tried:
{ "error": { "code": "SELECTOR_TIMEOUT", "message": "step 2/4 change failed on input[type=search]: ...", "request_id": "..." } }