browser_click
Automate clicking specific elements on web pages using CSS selectors for penetration testing, enhancing web application security assessments with precise browser interaction.
Instructions
Click an element on the page using CSS selector
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| selector | Yes | CSS selector for element to click |
Implementation Reference
- index.ts:259-299 (handler)The main handler for the 'browser_click' tool. It uses Playwright's page.locator to click the element specified by the CSS selector. Includes error handling for strict mode violations by retrying on the first matching element and returns appropriate success/error messages.case ToolName.BrowserClick: try { await page.locator(args.selector).click(); return { content: [{ type: "text", text: `Clicked: ${args.selector}`, }], isError: false, }; } catch (error) { if((error as Error).message.includes("strict mode violation")) { console.log("Strict mode violation, retrying on first element..."); try { await page.locator(args.selector).first().click(); return { content: [{ type: "text", text: `Clicked: ${args.selector}`, }], isError: false, }; } catch (error) { return { content: [{ type: "text", text: `Failed (twice) to click ${args.selector}: ${(error as Error).message}`, }], isError: true, }; } } return { content: [{ type: "text", text: `Failed to click ${args.selector}: ${(error as Error).message}`, }], isError: true, }; }
- index.ts:63-73 (registration)Registers the 'browser_click' tool in the TOOLS array, including its name, description, and input schema. This array is returned by the ListToolsRequest handler.{ name: ToolName.BrowserClick, description: "Click an element on the page using CSS selector", inputSchema: { type: "object", properties: { selector: { type: "string", description: "CSS selector for element to click" }, }, required: ["selector"], }, },
- index.ts:66-71 (schema)Defines the input schema for the 'browser_click' tool, specifying that it requires a 'selector' string parameter.inputSchema: { type: "object", properties: { selector: { type: "string", description: "CSS selector for element to click" }, }, required: ["selector"],
- index.ts:186-204 (helper)Helper function called by the handler to ensure a browser and page instance are available before performing the click action.async function ensureBrowser() { if (!browser) { browser = await playwright.firefox.launch({ headless: false }); } if (!page) { page = await browser.newPage(); } page.on("console", (msg) => { const logEntry = `[${msg.type()}] ${msg.text()}`; consoleLogs.push(logEntry); server.notification({ method: "notifications/resources/updated", params: { uri: "console://logs" }, }); }); return page!; }