Back to docs

Tool Evaluate Script

Category: Browser Automation · Tier: Free

Run JavaScript in the page context and return the result. The script runs as an expression.

Parameters

Parameter Type Required Description
script string Yes JavaScript to execute in the page context
_browserToken string No Browser session token

Examples

{ "script": "document.title" }
→ "CBrowser | Cognitive Browser Automation"

{ "script": "document.querySelectorAll('a').length" }
→ 42

{ "script": "(() => { const el = document.querySelector('h1'); return { text: el.textContent, tag: el.tagName }; })()" }
→ { "text": "Welcome", "tag": "H1" }

For complex scripts, wrap in an IIFE: (() => { ... return result; })().

Related

From the Blog