Browser

JavaScript Execution and Storage

ExecuteJavaScriptStr executes a JavaScript snippet and returns the result as a string. ExecuteJavaScriptObj returns the result as a dynamic object for accessing structured data. RunGet, RunSet, and RunDelete operate on browser storage types via the WebsiteStorageType enum. GetStorageData retrieves the result of a storage read operation. These methods are useful for manipulating page state that cannot be reached through the standard DOM interaction API.

WARNING

When the browser is driven via OttoMagic, ExecuteJavaScript depends on the browser extension's userScripts API (chrome.userScripts / browser.userScripts). In Chrome and Edge, this API is hidden until enabled once for the extension: 1. Open chrome://extensions (or edge://extensions). 2. Enable 'Developer mode' (top-right toggle). 3. Find the OttoMagic extension and click 'Details'. 4. Enable the 'Allow User Scripts' toggle. Firefox does not require this step. When driving the browser via Selenium or Puppeteer the script runs through IJavaScriptExecutor or Runtime.evaluate and this manual setup is not needed. Write the script either way: Selenium's executor is a function body and wants a return, while the other two evaluate a statement list and answer with its last value. GPAL runs it as written and, only if the engine refuses the return, wraps it in a function and runs it again.

Examples

GPAL Fluent: High-level fluent C# API

//JavaScript results are stored internally after ExecuteJavaScriptStr or ExecuteJavaScriptObj - retrieve them with GetStorageData. For structured JS results, use ExecuteJavaScriptObj and access properties on the dynamic result.

// Execute JavaScript and get a string result

string title;

browser

.GoTo("https://example.com")

.WithStorageKey("title")

.RunGet(WebsiteStorageType.localStorage)

.GetStorageData(out title);


// Clear localStorage before navigating

browser

.WithStorageKey("session")

.RunDelete(WebsiteStorageType.localStorage);


// Set a cookie value

browser

.WithStorageKey("theme")

.WithStorageData("dark")

.RunSet(WebsiteStorageType.cookie);

💬 Ask GPAL