browser_click
Simulate mouse clicks on web page elements using CSS selectors for automated browser interaction and testing.
Instructions
Click on an element specified by selector
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| selector | Yes |
Implementation Reference
- src/server.ts:164-189 (handler)The handler function that implements the core logic of the browser_click tool. It validates the selector input using Zod, ensures the Playwright browser is connected, retrieves the current page, executes page.click(selector), and returns a success message or error response in MCP format.async (params: any) => { try { const input = z.object({ selector: z.string() }).parse(params); await this.playwright.ensureConnected(); const page = this.playwright.getPage(); await page.click(input.selector); return { content: [{ type: 'text', text: `Successfully clicked element: ${input.selector}` }] }; } catch (error) { return { content: [{ type: 'text', text: `Click failed: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } }
- src/server.ts:155-190 (registration)The registration of the 'browser_click' tool in the MCP server using registerTool, including tool metadata (title, description), inline input schema, and the handler function.this.server.registerTool( 'browser_click', { title: 'Click Element', description: 'Click on an element specified by selector', inputSchema: { selector: z.string() } }, async (params: any) => { try { const input = z.object({ selector: z.string() }).parse(params); await this.playwright.ensureConnected(); const page = this.playwright.getPage(); await page.click(input.selector); return { content: [{ type: 'text', text: `Successfully clicked element: ${input.selector}` }] }; } catch (error) { return { content: [{ type: 'text', text: `Click failed: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } );
- src/types.ts:27-29 (schema)Zod input schema definition for the browser_click tool parameters (selector: string). Matches the inline schema used in registration.export const BrowserClickInputSchema = z.object({ selector: z.string() });
- src/playwright.ts:111-117 (helper)Helper method in PlaywrightManager called by the handler to ensure the browser connection is active before performing the click operation.async ensureConnected(): Promise<void> { const connected = await this.isConnected(); if (!connected) { await this.cleanup(); await this.connect(); } }
- src/playwright.ts:73-78 (helper)Helper method in PlaywrightManager to retrieve the current Playwright Page instance used for executing page.click().getPage(): Page { if (!this.page) { throw new Error('Playwright not connected. Call connect() first.'); } return this.page; }