Back to FAQs

Can I call a JavaScript function or extract a value from the page?

Sure thing! You can supply your Browser Bots with custom JavaScript code to run inside the browser. Your custom JavaScript can call functions, compute values, grab properties from the page or browser window, manipulate DOM elements, and even perform asynchronous programming and return a promise.

The way to do this is with Evaluate Blocks.

Evaluate Blocks, or eval blocks, are similar to Code Blocks except that they run inside the browser, while code blocks control your script’s from execution outside the browser. Because eval blocks run inside the browser, they have access to the DOM and to variables and functions within the page itself.

Using JavaScript to call a function or extract a value

You can use an eval block to extract a value from the page, which is mostly useful when you are debugging your script. You can also call a function on the page, which might be necessary if you need to imitate drag-and-drop or something like that that is difficult to do with ordinary browser automation.

Using JavaScript to manipulate DOM elements

Once in a while, you might need to test a web application with an element that isn’t easily manipulated with normal Browser Bot steps, so you’ll need to execute JavaScript in the bot’s browser window to do it. Examples of when this might be necessary are for range sliders or color pickers.

Using JavaScript for custom validation

One use for eval blocks is to write custom validation, throwing an error if expected content on the page isn’t found.

var welcomeMessage = document.querySelector('.welcome-message');

if (!welcomeMessage) {
    throw new Error("No welcome message found after logging in!");

Learn more and check out the examples in Evaluate Blocks.