Skip to content

Evaluate JS

Type: page-evaluate  ·  Package: Browser v1.0.9  ·  Output: object

Runs JavaScript code in the context of the page and returns the result. It’s an advanced activity: it gives you direct access to the DOM and the page’s APIs for cases the other activities don’t cover — reading a computed value, handling a tricky element, or calling one of the page’s own functions.

It runs the script on the page. It can be an expression (document.title) or a function that receives an argument ((arg) => arg.total * 2), in which case the value of the arg parameter is passed to it. If the script returns a promise, the activity waits for it to resolve. It returns whatever the script returns.

  • The script is written as real JavaScript, without quotes around it: it runs in the browser, not in the expression engine. Playbook values are passed through arg, not concatenated into the code.
  • arg must be serializable (text, number, boolean, list or dictionary).
  • It doesn’t wait for any element and has no timeout: if the script looks for something that isn’t there yet, put a Wait For first.
  • The result isn’t written to the log (it may contain page data): the log only says whether there was a result.
ParameterTypeDefaultDescription
scriptstring—JavaScript expression or function. E.g. document.title or (arg) => arg.total * 2.
ParameterTypeDefaultDescription
pageZoanPageSession—The page session. Omit it inside a body; outside, pass the session variable (e.g. page = page).
argobject—Argument passed to the script if it is a function.

Returns an object with the value the script returns, serialized: text, number, boolean, list or object. If the script returns nothing (undefined) or returns null, the output is empty. A DOM element is of no use outside the page: return its text or its attributes.

Page Evaluate script = document.title → output: titulo
Page Evaluate
script = () => Array.from(document.querySelectorAll('.price')).map(e => e.innerText)
→ output: precios
For Each items = precios itemVariable = precio
└─ activities:
Log message = "Price: " + precio

Pass a playbook value with arg:

Page Evaluate script = (code) => document.querySelector(`[data-code="${code}"]`)?.innerText arg = codigoProducto → output: descripcion
MessageCause and fix
'script' is required.The script is empty.
JavaScript error (e.g. ReferenceError: x is not defined, TypeError: Cannot read properties of null)The script failed inside the page. Try it first in the browser console (F12) and guard access to elements that may not exist (?.).
'page' must be a ZoanPageSession.page is empty outside a body.