JavaScript Instructions

Interact with the website you want to scrape and get an optimal response.

This parameter only works with render_js=1

Interacting with the website you are scraping is very important, especially if you want to retrieve custom information. WebScrapingAPI allows you to do so, by simply adding the js_instructions parameter to your request.

This parameter's value is a stringified object, which accepts the following options:

ParameterTypeDescription

action Required

string

The action that you want to perform. Accepted values are:

-click - click on an element specified by the selector option -click_and_navigate - same as click, except it also handles the page navigation that is triggered by the click

-scrollTo - scroll to an element specified by the selector option

-scrollPx - scroll a specified distance in pixels specified by the pixels option

-scrollInf - scroll a specified number of times specified by the count option

-delete_element - delete the first element found for a given selector. If the multiple property on the instuction is true, it will delete all the elements.

-focus - sets the focus on a HTML element

-value - sets the value of a HTML input

-typed_value - same as value, but the value is typed like a user would

-submit - works only on FORM elements -submit_and_navigate - same as submit, except it also handles the page navigation that is triggered by the submit -solve_captcha - solve any reCaptcha or hCaptcha challenge. - wait_for_css - wait for a CSS selector to exist in the page.

selector

string

The CSS selector on which you want to perform your action.

block

string

The area of the HTML element identified by the specified CSS selector. Used with the scrollTo action. Accepted values are start, center and end. The default value is start.

value

string

The value of a HTML input identified by the specified CSS selector. Used with the value action.

pixels

string

The distance for vertically scrolling. Used with the scrollPx action.

count

int

The number of times to perform a vertically scroll. Used with the scrollInf action. The scrolling distance is calculated by the window height. Useful for lazy-loaded lists.

timeout

int

Perform a delay after each JavaScript instruction. For the scrollInf action, the delay is performed after each individual scroll. For the wait_for_css action, it represents to time to wait for the CSS selector to appear and it defaults to 5 seconds. Measured in ms.

strict

string

By default, the JavaScript instructions are executed in "strict mode", which means that if an error occurs during the execution of one instruction, the request will be aborted and an error will be returned.

You have the option to deactivate this behaviour by assigning the strict key the value "false" within your instruction.

A full example of how this parameter would look like in production is:

js_instructions='[{"action":"scrollTo","selector":"div.navFooterBackToTop","timeout": 5000, "block": "start"}]'

The js_instructions parameter will accept a URL encoded JSON string that represents an array. This is a mock example that uses multiple actions:

[{"action":"click","selector":"<selector>","timeout":2000, "strict":"false"},{"action":"click","selector":"<selector>","timeout":2000, "strict":"true"},{"action":"click","selector":"<selector>","timeout":2000},{"action":"scrollTo","selector":"<selector>","timeout": 2000, "block": "end"}]

And this is the URL encoded value from the example above:

%5B%7B%22action%22%3A%22click%22%2C%22selector%22%3A%22%3Cselector%3E%22%2C%22timeout%22%3A2000%2C%20%22strict%22%3A%22false%22%7D%2C%7B%22action%22%3A%22click%22%2C%22selector%22%3A%22%3Cselector%3E%22%2C%22timeout%22%3A2000%2C%20%22strict%22%3A%22true%22%7D%2C%7B%22action%22%3A%22click%22%2C%22selector%22%3A%22%3Cselector%3E%22%2C%22timeout%22%3A2000%7D%2C%7B%22action%22%3A%22scrollTo%22%2C%22selector%22%3A%22%3Cselector%3E%22%2C%22timeout%22%3A%202000%2C%20%22block%22%3A%20%22end%22%7D%5D

JavaScript Instructions Integration Examples

GET https://api.webscrapingapi.com/v1

The following examples shows how the js_instructions parameter is used in order to interact with the scraped url, before retrieving the HTML code.

Query Parameters

NameTypeDescription

api_key*

String

<YOUR_API_KEY>

url*

String

https://webscrapingapi.com

render_js

Integer

1

js_instructions

Object

[{"action":"scrollTo","selector":"div.navFooterBackToTop","timeout": 5000, "block": "start"}]

The full GET request for the js_instructions should be:

https://api.webscrapingapi.com/v1?api_key=<YOUR_API_KEY>&url=https://webscrapingapi.com&js_instructions=%5B%7B%22action%22%3A%22scrollTo%22%2C%22selector%22%3A%22div.navFooterBackToTop%22%2C%22timeout%22%3A%205000%2C%20%22block%22%3A%20%22start%22%7D%5D%0A
curl "https://api.webscrapingapi.com/v1?api_key=<YOUR_API_KEY>&url=https://webscrapingapi.com&render_js=1&js_instructions=%5B%7B%22action%22%3A%22scrollTo%22%2C%22selector%22%3A%22div.navFooterBackToTop%22%2C%22timeout%22%3A%205000%2C%20%22block%22%3A%20%22start%22%7D%5D%0A"

Important! The url & js_instructions parameters have to be encoded. ( i.e. &url=https%3A%2F%2Fwww.webscrapingapi.com%2F&js_instructions=%5B%7B%22action%22%3A%22scrollTo%22%2C%22selector%22%3A%22div... )

Last updated