Skip to main content
Glama

browser_execute_script

Execute JavaScript code directly in a web browser to automate interactions, manipulate page elements, or retrieve dynamic content during web automation tasks.

Instructions

Execute JavaScript in the context of the current page

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYesJavaScript code to execute

Implementation Reference

  • Handler function for the browser_execute_script tool. It uses StateManager to get the WebDriver, instantiates ActionService, and calls its executeScript method with the provided script.
    async ({ script }) => { try { const driver = stateManager.getDriver(); const actionService = new ActionService(driver); const result = await actionService.executeScript(script); return { content: [{ type: 'text', text: `Script executed successfully: ${result}` }], }; } catch (e) { return { content: [ { type: 'text', text: `Error executing script: ${(e as Error).message}`, }, ], }; } }
  • Input schema for browser_execute_script tool defining the 'script' parameter as a string.
    { script: z.string().describe('JavaScript code to execute'), },
  • Registration of the browser_execute_script tool via server.tool() call within registerActionTools function.
    server.tool( 'browser_execute_script', 'Execute JavaScript in the context of the current page', { script: z.string().describe('JavaScript code to execute'), }, async ({ script }) => { try { const driver = stateManager.getDriver(); const actionService = new ActionService(driver); const result = await actionService.executeScript(script); return { content: [{ type: 'text', text: `Script executed successfully: ${result}` }], }; } catch (e) { return { content: [ { type: 'text', text: `Error executing script: ${(e as Error).message}`, }, ], }; } } );
  • Helper method in ActionService class that executes the JavaScript script using the Selenium WebDriver's executeScript method.
    async executeScript(script: string, args = []): Promise<any> { return this.driver.executeScript(script, ...args); }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/pshivapr/selenium-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server