Web Browser - Inject JavaScript

Declaration

<AMWEBBROWSER ACTIVITY="inject_javascript" SESSION="text" SCRIPT="text" FILENAME="text" RESULTDATASET="text" />

Related Topics

Description

Injects JavaScript code into a web page to by way of embedded code, or an external .js or .txt file.

NOTE:
  • The Web Browser action is not backwards compatible with the Web Browser (Legacy) action.
  • Automation with the Web Browser action is only compatible with Internet Explorer, Chrome, Edge, and Firefox browsers.
  • You must update the specified browser to the latest version and install it on the target system to ensure the Web Browser action functions correctly.
  • Unlike other session-based actions where sessions are optional, the Web Browser action requires the use of a session as each created session explicitly links to a specific web browser instance (you can create a session using the Web Browser - Create session or Web Browser - Open activity). A single task can support numerous sessions; however, since Web Browser activities are performed interactively (and not in the background), each session must run concurrently to avoid sending interactions to the wrong browser window.

Practical usage

See Description.

Parameters

Browser

Property Type Required Default Markup Description
Session name Text Yes BrowserSession1 SESSION="mySession" Specifies the name of an existing session to associate with this activity. Numerous sessions can exist within a single task allowing activities to be performed on several web browsers simultaneously.
NOTE:
  • This activity supports Web Browser sessions in Chrome, Edge, and Firefox browsers only – Internet Explorer is not supported.
  • Firefox does not require a separate extension to be installed with this version of the Web Browser action.

General

Property Type Required Default Markup Description
Embedded Text Yes, if selected (Empty) SCRIPT="var images = document.getElementsByTagName('img'); &#xD;&#xA;&#xA;var srcList = [];&#xD;&#xA;&#xA;for(var i = 0; i &lt; images.length; i++) {&#xA; images[i].style.filter = &quot;blur(10px)&quot;;&#xA;}" If selected, specifies that the script is embedded in the task (enabled by default). Enter the script in the provided box. If this parameter is selected, the External parameter becomes inactive.
External Text Yes, if selected (Empty) FILENAME="C:\temp\samplescript.js" If selected, specifies that the javascript code is located in an external .js or .txt file (disabled by default). Click the Folder icon to navigate to the desired external file or manually enter the full path and file name in the provided box.  If this parameter is selected, the Embedded parameter becomes inactive.
Create and populate dataset with execution results Text No (Empty) RESULTDATASET="theDataset" The name of the dataset to create and populate with the JavaScript code execution results. For information regarding the individual dataset fields this activity creates, see Datasets.

Description

Error Causes

On Error

Additional Information

Datasets

A dataset is a multiple column, multiple row container object. This activity creates and populates a dataset containing a specific set of fields in addition to the standard dataset fields. The table below describes these fields (assuming the dataset name assigned was theDataset).

Name Type Return Value
theDataset.Result Column Returns the primitive value of the JavaScript execution code.
theDataset.<dynamic column names> Column Returns the values of each corresponding field within the complex object.
theDataset.Error Column Returns an error found in the JavaScript execution code, if it exists.

Examples

NOTE:
  • Copy and paste the sample AML code below directly into the Task Builder Steps Panel.
  • To successfully run the sample code, update parameters containing user credentials, files, file paths, or other information specific to the task to match your environment.

Example 1

This sample task opens the Chrome web browser, goes to google.com, and then blurs all images on the google.com web page.

Copy
<AMWEBBROWSER URL="google.com" SESSION="BrowserSession1" BROWSER="chrome" />
<AMWEBBROWSER ACTIVITY="inject_javascript" SESSION="BrowserSession1" SCRIPT="var images = document.getElementsByTagName('img'); &#xD;&#xA;&#xA;var srcList = [];&#xD;&#xA;&#xA;for(var i = 0; i &lt; images.length; i++) {&#xA;    images[i].style.filter = &quot;blur(10px)&quot;;&#xA;}" />

Example 2

This sample task opens the Chrome web browser, goes to https://www.ihbristol.com/english-phrases, extracts the "Phrase of the date" and its description, and then creates and populates a dataset with the results.

Copy
<AMWEBBROWSER URL="https://www.ihbristol.com/english-phrases" SESSION="BrowserSession1" BROWSER="chrome" />
<AMWEBBROWSER ACTIVITY="inject_javascript" SESSION="BrowserSession1" SCRIPT="function CreateDataset() {&#xD;&#xA;    let phraseElem = document.getElementsByClassName(&quot;field-name-title&quot;);&#xD;&#xA;    let phraseText = phraseElem[0].textContent;&#xD;&#xA;    let phraseExplanationElem = document.getElementsByClassName(&quot;field-name-field-phrase-explanation&quot;);&#xD;&#xA;    let phraseExplanationText = phraseExplanationElem[0].textContent;&#xD;&#xA;&#xA;    var dataset = {&#xA;    &quot;Phrase&quot;: phraseText,&#xA;    &quot;Explanation&quot;: phraseExplanationText&#xA;    };&#xA;&#xA;    return dataset ;&#xA;}&#xD;&#xA;&#xD;&#xA;var result = CreateDataset();&#xD;&#xA;&#xD;&#xA;return result;" RESULTDATASET="theDataset" />