Skip to main content

Run JavaScript on a webpage

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)

NameDescriptionData TypeRequired?Example
Browser session IDThe unique identifier of the browser instance. Can be retrieved from "Open a browser session" Wrk ActionText (Short)Yesfb9bc380-146c-420e-9130-50ce93614e05
JavaScript codeJavaScript to execute in the browser session connected to.Text (Long)Yesconsole.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)

NameDescriptionData TypeRequired?Example
Return valueValue returned by the JavaScript executed.Text (Long)Yes
Unsuccessful messageIf unsuccessful, a message stating what went wrongText (Long)Yes
Unsuccessful screenshotIf unsuccessful, a screenshot of the websiteFileYes

Outcomes

NameDescription
SuccessThis status is selected in the event that the JavaScript provided has been executed
UnsuccessfulThis status is selected in the event of the following scenario:An error occurred running the JavaScript provided
Impossible to completeThis 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

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

Focus on an element

document.querySelector().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';

}

}