Skip to main content
Run JavaScript on a webpage
Bassem Khalifa avatar
Written by Bassem Khalifa
Updated over a week ago

Run JavaScript on a webpage.

Note: This JavaScript will run on the browser session that it is attached to. It will have full access to the website you are visiting. I can be used to retrieve information and interact with the website in any way necessary for your Wrkflow.

Application

  • Web Process Automation

Inputs (what you have)

Name

Description

Data Type

Required?

Example

Browser session ID

The unique identifier of the browser instance. Can be retrieved from "Open a browser session" Wrk Action

Text (Short)

Yes

fb9bc380-146c-420e-9130-50ce93614e05

JavaScript code

JavaScript to execute in the browser session connected to.

Text (Long)

Yes

console.log("hello");

Note: the value of inputs can either be a set value in the configuration of the Wrk Action within the Wrkflow, or a variable from the Data library section. These variables in the Data library section are the outputs of previous Wrk Actions in the Wrkflow.

Outputs (what you get)

Name

Description

Data Type

Required?

Example

Return value

Value returned by the JavaScript executed.

Text (Long)

Yes

Unsuccessful message

If unsuccessful, a message stating what went wrong

Text (Long)

Yes

Unsuccessful screenshot

If unsuccessful, a screenshot of the website

File

Yes

Outcomes

Name

Description

Success

This status is selected in the event that the JavaScript provided has been executed

Unsuccessful

This status is selected in the event of the following scenario:

  • An error occurred running the JavaScript provided

Impossible to complete

This status is selected in the event of the following scenario:

  • Unable to connect to the session for some reason

Requirements

  • In order to create strings in your code make sure you use single quotes (') instead of double quotes ("). Double quotes will require you to escape them (\") in your code.

Common JavaScript functions

Navigate to a new website:

window.location.href = 'example.com';

Focus on an element

document.querySelector(<CSS Selector>).focus();

Scroll to the bottom of the page

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

Scroll to an element on a page

document.querySelector(selector).scrollIntoView({ behavior: 'smooth', block: 'start' });

Paste a value into an element

document.querySelector('selector').value = 'Your Text';

How to return data using JavaScript

If you are looking to run a JavaScript function and return the response to the Wrkflow you will need to structure your code as a function. Whatever is returned from the function will be captured by the Wrk Action. In a simple example below I will capture the inner text of the h5 element and return it to the Wrkflow

() => {return document.querySelector("h5")?.innerText}

Additionally, you can create a named function that the WA will run:

function getH5InnerText() {

var h5Element = document.querySelector('h5');

if (h5Element) {

return h5Element.innerText;

} else {

return

'No h5 element found';

}

}

Did this answer your question?