Invoke Javascript - Activities - Foundation 23.2 - Foundation 23.2 - Ready - Hyland RPA - external - Hyland-RPA/Activities/Foundation-23.2/Hyland-RPA-Activities/Developer/Web/Java-Script/Invoke-Javascript - 2025-04-03

Hyland RPA Activities

Platform
Hyland RPA
Product
Activities
Release
Foundation 23.2
License

Developer.Web.Javascript

Description

Executes the JavaScript code in the browser. For example, you can retrieve information from a website and store it in a variable.

Properties

Analyst

  • Description: Optional text for documentation purposes.

Common

  • Continue On Error: Continues the execution of the workflow even if an error occurs during the execution of the activity.

  • Delay Before: Delay time in milliseconds before the activity is executed. The default value is 250 milliseconds.

  • Delay After: Delay time in milliseconds after executing of the activity. The default value is 250 milliseconds.

  • Disable Log: Disables the logging functionality for this activity.

  • Disable Protocol: Disables the protocol functionality for this activity.

  • Exclude from Global Delay: Excludes this activity from the Global Delay functionality.

  • Timeout: Duration in milliseconds in which the activity tries to execute.

Input

  • Java Script: JavaScript function to execute. You need to place your JavaScript code inside an anonymous function: function(){//your JavaScript code}
  • Parameters: Provide the input parameters to use in your JavaScript code and make sure to pass these parameters to your function, for example function(_username, _password){//your JavaScript code}

Expert

  • Browser: Browser where you want to execute the Invoke Javascript activity. Ensure to provide this input if you want to use the activity in the window scope.

Output

  • Script Output: Execution result, that is the return value of the JavaScript function.
    Note: You can only return the variable types that implement the IConvertible interface, for example Boolean, String, Int32, Double, but not String[].

Example

The following JavaScript code extracts the links and descriptions as well as the old and new prices of all search results for a specific item, for example a screen, on Amazon and returns the resulting array as a string.

To see what this code does, complete the following steps in Hyland RPA Designer:

  1. Open Amazon using the Open or Attach Browser activity.
  2. Search for a product, for example a screen.
  3. Use the following code to retrieve the information mentioned above.
    var productArr = [];
    var products = Array.from(document.querySelectorAll('div[data-cel-widget^=""search_result_""]'));
    products.forEach(function(item) {
    var articleLink = item.querySelector('span.a-text-normal').parentElement.href;
    var articleDescription = item.querySelector('span.a-text-normal');
    var articleDiscountPrice = item.querySelector('span.a-price-whole');
    var nodesNormalPrices = item.querySelectorAll('span.a-offscreen');
    var articleNormalPrice = nodesNormalPrices[nodesNormalPrices.length - 1];
    if (articleLink != null && articleDescription != null && articleDiscountPrice != null && articleNormalPrice != null)
       {
       productArr.push(`${articleLink};; ${articleDescription.innerHTML};; ${articleDiscountPrice.innerHTML};; ${articleNormalPrice.innerHTML}`);
       }
       });
    return JSON.stringify(productArr);
    }"
  4. Log the output of the activity.

You can use the Invoke JavaScript activity both in the browser and in the window scope. In the latter case, however, you must specify the currently open browser as an input argument.

Tips and Tricks

  • Use the Invoke JavaScript activity only to retrieve or set elements you cannot access using common activities. Try to use as little logic as possible in your JavaScript code, as it should not substitute business logic that should be handled directly in your process.
  • Use the developer tools of your browser to test your code directly in the browser. This is especially helpful the longer and more complex your code becomes.
  • Log all relevant steps, for example retrieved input data, as you normally would.

    Example

    log(`The following text was read out: ${retrievedText}`) 

Troubleshooting

  • Issue
    Core.AutomationLibraries.WebAutomationBase.JavaScriptInjectionException: An exception was caught executing Java Script injection javascript error: missing ) after argument list:
    This exception is connected to an invalid JavaScript syntax. Examples include incorrect capitalizations, for example Function instead of function, or missing parentheses.
  • Issue
    InvalidCastException: An object must implement IConvertible:
    This error appears if you selected an invalid variable type as script output. For a complete list of the accepted variable types, see IConvertible Interface on docs.microsoft.com.
  • Issue
    Core.AutomationLibraries.AutomationLibraryBase.Exceptions.BrowserNotFoundException: Failed to locate Browser:
    The browser could not be located. Check if you added the correct browser in the Browser property. For this to work, you must return this browser variable in the enclosing Open Or Attach Browser activity in the first step.

See also