Skip to main content

Execute JavaScript

Command description

Execute JavaScript on the specified webpage. Arguments and return values are serialized automatically, and asynchronous functions and promises are supported.

To execute JavaScript inside an iframe, specify any element contained in that iframe.

Maximum Execution Time can be configured in Advanced settings. The command reports an error if the function does not return before the timeout.

Command Input Parameters

input parametersInput parameter typeDescription
Webpage ObjectWebPageExecute JS code on this web page
Web ElementWebElement or str (captured element UID, not element name, not ms-rpa-id)Optional; execute inside the frame containing this element. Select a captured element or one returned by Get Web Element, Get Similar Web Elements, or Get Related Web Elements
Input ArgumentsPythonExpressionArguments passed to the webpage's JavaScript function must be Python expressions and may use Python literals. They are automatically serialized to JSON and then deserialized into JavaScript objects. The default is Python None.
JavaScript CodeJavaScript CodeJS code to be executed
Maximum Execution TimenumberMaximum time allowed for the JavaScript code to return, in seconds

Input Arguments is the exact parameter name. In workflow JSON, for example, use "Input Arguments": "[1, 2, 3]".

Command Output Parameters

Output parametersOutput parameter typeDescription
JavaScript ResultanyJavaScript return value, deserialized into a Python object

Type definition reference

JS code template

async function(htmlElement, param) {
//Write your Javascript code here
//htmlElement is the JS HTMLElement object corresponding to the web page element in the input parameter
//param is the input parameter. The Python object will be serialized into JSON first, and then deserialized into the JS param parameter.
//The return value can be an object and supports returning Promise
return null
}
tip

Install the Runavelo browser extension before using this command.

Example 1

Check Taobao homepage sales ranking

js code

async function (htmlElement, param) {
const goods = []
const elements = document.querySelectorAll('.tb-pick-content-item')
for (let element of elements) {
const name = element.querySelector('.info-wrapper').innerText
const price = parseInt(element.querySelector(".price-value").innerText)
const count = parseInt(element.querySelector(".month-sale").innerText)
goods.push({
name,
price,
count
})
}
return goods
}

Example 2

Execute JavaScript in the iframe containing the Image 1 element to hide it.

JSON workflow parameter value rules

In a JSON workflow, Input Arguments is a Python expression. Write string literals as "'text'"; write lists and maps as Python expressions, for example "[1, 2, 3]" and "{'name': 'John Doe'}".

in["JavaScript Code"] is JavaScript source text. Enter the function directly without wrapping it in Python string quotes.

  • Correct example: "JavaScript Code": "async function(htmlElement, param) { return param }"
  • Error example: "JavaScript Code": "'async function(htmlElement, param) { return param }'"
{
"children": [],
"in": {
"Webpage Object": "webPage",
"Web Element": "'0f09d6d5-e7c7-4952-910f-5486263be36d'",
"Input Arguments": "[1,2,3]",
"JavaScript Code": "async function(htmlElement, param) { return param }",
"Maximum Execution Time": "10"
},
"out": {
"JavaScript Result": "jsResult"
},
"ins": "Execute JavaScript"
}

FAQ

See Web Automation FAQ