Run JavaScript

Runs JavaScript code on a web page.

Command availability: IBM RPA SaaS and IBM RPA on premises

Script syntax

IBM RPA's proprietary scripting language has a syntax similar to other programming languages. The script syntax defines the command's syntax in the script file. You can work with this syntax in IBM RPA Studio's Script mode.

webExecuteJavaScript --script(String) (String)=value

Dependencies

Use the Start Browser (webStart) command to start a browser or use the Find Internet Explorer Browser (findBrowser) command to attach the Microsoft Internet Explorer before using the Run JavaScript (webExecuteJavaScript) command.

Input parameter

The following table displays the list of input parameters available in this command. In the table, you can see the parameter name when working in IBM RPA Studio's Script mode and its Designer mode equivalent label.

Designer mode label Script mode name Required Accepted variable types Description
JavaScript Code script Required Text JavaScript code to run.
Note:Use the return keyword to obtain the return value.

Output parameter

Designer mode label Script mode name Accepted variable types Description
Value value Text, Number Return the value from the return JavaScript function.

Example

Example 1: Use the Run JavaScript (webExecuteJavaScript) command to run the script and show an alert.

webStart --name browser --type "Chrome"
// The Run JavaScript command shows an alert in the browser.
webExecuteJavaScript --script "window.alert(\'Hello world\');"
webClose --name browser --leavebrowseropen

Example 2: Use the Run JavaScript (webExecuteJavaScript) command to run the script and return the accessed URL.

defVar --name URL --type String
webStart --name browser --type "Chrome"
webNavigate --url "https://www.ibm.com"
webExecuteJavaScript --script "return window.location.href;" URL=value
logMessage --message "${URL}" --type "Info"
webClose --name browser --leavebrowseropen