Execute Javascript

<< Click to Display Table of Contents >>

Navigation:  Actions > Web Automation >

Execute Javascript

Enter topic text here.The Execute JavaScript action runs custom JavaScript code in the browser. This powerful action allows you to interact with the page in ways that might not be covered by other actions.

 

When to use this action:

Extracting data from complex JavaScript-rendered pages

Bypassing limitations of standard actions (hidden elements, shadow DOM)

Performing calculations or data transformations on page content

Modifying page appearance before screenshots (hiding banners, popups)

Handling special scenarios like infinite scroll or lazy loading

Accessing page variables or JavaScript functions

Note: This action requires JavaScript programming knowledge. Users without programming skills can accomplish most tasks using other actions.

 

WA_ExecJavaScript

 

Browser

Select which browser to execute the script in

 

Inline Script

Type or paste JavaScript directly into the text area

Best for short scripts or quick operations

Supports multi-line code with full JavaScript syntax

 

JavaScript File

Load script from an external .js file

Click [...] to browse for the file

Good for complex or reusable scripts

 

Save Result to Variable

Optional: Enter a variable name to store the script's return value

Your script must use return statement to send a value back

Leave empty if the script doesn't return anything

 

How to Return Values

To save a result to a variable, your script must use the return statement:

 

// Get page title

return document.title;

 

// Count elements

return document.querySelectorAll('.item').length;

 

// Get multiple values as JSON

return {

   title: document.title,

   url: window.location.href,

   timestamp: new Date().toISOString()

};

 

Examples of usage.

Extract data from a table:

const rows = document.querySelectorAll('table tr');

const data = [];

rows.forEach(row => {

   const cells = row.querySelectorAll('td');

   if (cells.length >= 2) {

       data.push({

           name: cells[0].textContent,

           value: cells[1].textContent

       });

   }

});

return JSON.stringify(data);

 

Scroll to bottom of page:

window.scrollTo(0, document.body.scrollHeight);

// No return needed for actions without results

 

 

note Related Topics

Start Browser

Close Browser

Navigate

Mouse Click / Hover / Focus

Click Download Link

Send Keys

Get Element Property

Get Page Details

Set Form Field Value

Take Screenshot