Back to FAQs

Can I extract a value or call a function on the page?

Sure thing! Browser Bots can run arbitrary code inside the browser 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 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.

Extracting a simple value

The simplest eval blocks return a single value from the page. You can eval 1 + 1 to get 2, or you can eval window.location.href to get the URL of the current page. The last (or only) value in your evaluate is what gets returned. Extracting a simple value like this is probably only useful for debugging your script.

Evaluating a promise

If you craft an eval block that returns a promise, Loadster will automatically wait for the promise to resolve. This is crucial because it enables asynchronous programming and long-running operations within the eval block.

Here is an example of an eval block that connects to a WebSocket server and returns the 3rd message it receives, an operation that may take some time.

new Promise((resolve, reject) => {
  const socket = new WebSocket("");

  let messagesReceived = 0;

  socket.onmessage = (event) => {

    if (messagesReceived > 3) {

Including an eval block like this in your script will effectively block the script until the promise is resolved (in this case, by receiving the 3rd message from the WebSocket).

Throwing an error

If your eval block throws an error, Loadster will catch the error and raise it as a script error. This is a nice way to add custom validation to your browser scripts.

var errors = document.querySelectorAll('error-msg');

if (errors.length > 0) {
  throw new Error("The page is showing an error: " + errors[0].innerText);

Likewise, you can throw an error if something you expect is not found on the page.

if (!document.getElementById('login-success')) {
  throw new Error("Login failed!");

Calling a function

From an eval block, you can call any function available to the page, just as you could in your browser’s JavaScript console.

This could be something simple like console.log() which will log something to the JavaScript console, which you can then see in the script logs when playing in single-user mode.

You could also use an eval block to call a custom function on your page:

  product: "product-38a64b",
  quantity: 1,
  destination: 92091

In a typical script, this is not necessary, since you will be interacting with the site with normal user operations (navigating, clicking on elements, entering text into fields, and so on) and site behaviors will be triggered by those actions. But eval blocks can be quite helpful when you need to shim custom behavior on the page.