browser_execute_script
Execute JavaScript code within the current web page to automate browser interactions, manipulate page elements, or perform custom web testing operations.
Instructions
Execute JavaScript in the context of the current page
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| script | Yes | JavaScript code to execute |
Implementation Reference
- src/tools/actionTools.ts:230-255 (handler)The complete tool registration block for 'browser_execute_script', serving as the handler function that validates input with Zod schema, executes the script via ActionService, and returns success/error responses.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}`, }, ], }; } } );
- src/tools/actionTools.ts:233-235 (schema)Input schema for the browser_execute_script tool using Zod.{ script: z.string().describe('JavaScript code to execute'), },
- src/services/actionService.ts:65-67 (helper)Core helper method in ActionService that executes arbitrary JavaScript on the current page using Selenium WebDriver's executeScript.async executeScript(script: string, args = []): Promise<any> { return this.driver.executeScript(script, ...args); }
- src/tools/index.ts:11-11 (registration)Invocation of registerActionTools within registerAllTools, which registers the action tools including browser_execute_script.registerActionTools(server, stateManager);