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
| Name | Required | Description | Default |
|---|---|---|---|
| script | Yes | JavaScript code to execute |
Implementation Reference
- src/tools/actionTools.ts:236-254 (handler)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}`, }, ], }; } }
- src/tools/actionTools.ts:233-235 (schema)Input schema for browser_execute_script tool defining the 'script' parameter as a string.{ script: z.string().describe('JavaScript code to execute'), },
- src/tools/actionTools.ts:230-255 (registration)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}`, }, ], }; } } );
- src/services/actionService.ts:65-67 (helper)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); }