Web Browser - Inject JavaScript
Declaration
<AMWEBBROWSER ACTIVITY="inject_javascript" SESSION="text" SCRIPT="text" FILENAME="text" RESULTDATASET="text" />
Description
Injects JavaScript code into a web page to by way of embedded code, or an external .js or .txt file.
- 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:
|
General
Property | Type | Required | Default | Markup | Description |
---|---|---|---|---|---|
Embedded | Text | Yes, if selected | (Empty) | SCRIPT="var images = document.getElementsByTagName('img'); 

var srcList = [];

for(var i = 0; i < images.length; i++) {
 images[i].style.filter = "blur(10px)";
}" | 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. |
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
- 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.
<AMWEBBROWSER URL="google.com" SESSION="BrowserSession1" BROWSER="chrome" />
<AMWEBBROWSER ACTIVITY="inject_javascript" SESSION="BrowserSession1" SCRIPT="var images = document.getElementsByTagName('img'); 

var srcList = [];

for(var i = 0; i < images.length; i++) {
 images[i].style.filter = "blur(10px)";
}" />
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.
<AMWEBBROWSER URL="https://www.ihbristol.com/english-phrases" SESSION="BrowserSession1" BROWSER="chrome" />
<AMWEBBROWSER ACTIVITY="inject_javascript" SESSION="BrowserSession1" SCRIPT="function CreateDataset() {
 let phraseElem = document.getElementsByClassName("field-name-title");
 let phraseText = phraseElem[0].textContent;
 let phraseExplanationElem = document.getElementsByClassName("field-name-field-phrase-explanation");
 let phraseExplanationText = phraseExplanationElem[0].textContent;

 var dataset = {
 "Phrase": phraseText,
 "Explanation": phraseExplanationText
 };

 return dataset ;
}

var result = CreateDataset();

return result;" RESULTDATASET="theDataset" />