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" }]When steps run
Section titled “When steps run”- The page loads, then ScrapeNinja waits for
waitForSelectorandpostWaitTime, if you set them. - The steps run one by one, in order. Each step waits until its element is visible, scrolls it into view and acts on it.
- If a step fails, the whole attempt fails and is retried from another proxy (up to
retryNum), like any other error. - After the last step:
catchAjaxHeadersUrlMask, the screenshot, the page HTML and your extractor.
Step types
Section titled “Step types”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). |
Selectors
Section titled “Selectors”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.
Typing and selecting
Section titled “Typing and selecting”change behaves like a person:
- on a
<select>, the option whosevalueequalsvalueis 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,
valueis set directly andinputandchangeevents are fired.
Catching an XHR
Section titled “Catching an XHR”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.
Recording steps with Chrome DevTools
Section titled “Recording steps with Chrome DevTools”You don’t have to write steps by hand. Chrome can record them:
- Open the target page in Chrome, then DevTools → Recorder (⋮ → More tools → Recorder).
- Click Create a new recording, do what you need on the page, stop the recording.
- Export it as JSON and copy the
stepsarray into your ScrapeNinja request. - Keep only the
click,changeandwaitForElementsteps. RemovesetViewport,navigate,keyDown/keyUpand 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.
Full example
Section titled “Full example”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 }'import requests
res = requests.post( "https://scrapeninja.apiroad.net/v2/scrape-js", headers={"X-Apiroad-Key": "YOUR_KEY"}, json={ "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, }, timeout=120,)print(res.json()["extractor"]["result"]["markdown"][:500])const res = await fetch('https://scrapeninja.apiroad.net/v2/scrape-js', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Apiroad-Key': 'YOUR_KEY' }, body: JSON.stringify({ 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, }),});console.log((await res.json()).extractor.result.markdown.slice(0, 500));Limits and errors
Section titled “Limits and errors”- Up to 50 steps per request.
- Each step has 11 seconds to find a visible element and act. The request
timeoutstill applies to the whole attempt, so give long step lists a highertimeout. - If an element exists but stays hidden for 3 seconds (for example a link inside a collapsed menu), a
clickfalls back to a DOM click on it. - A step that can’t find its element fails the attempt with
SELECTOR_TIMEOUTand 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": "..." } }