click
Simulate mouse clicks on web page elements using CSS selectors to automate browser interactions for testing or workflow automation.
Instructions
Click an element on the page
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| selector | Yes | CSS selector for the element | |
| button | No | Mouse button to click | left |
| clickCount | No | Number of clicks | |
| delay | No | Delay between mousedown and mouseup in ms | |
| timeout | No | Timeout in milliseconds | |
| tabId | No | Tab ID to operate on (uses active tab if not specified) |
Implementation Reference
- src/schemas.ts:51-58 (schema)Zod input schema for the click tool defining parameters: selector (required), button (default 'left'), clickCount (1-3, default 1), delay (0-5000ms), timeout, tabId.export const clickSchema = z.object({ selector: selectorSchema, button: z.enum(['left', 'right', 'middle']).optional().default('left').describe('Mouse button to click'), clickCount: z.number().int().min(1).max(3).optional().default(1).describe('Number of clicks'), delay: z.number().int().min(0).max(5000).optional().describe('Delay between mousedown and mouseup in ms'), timeout: timeoutSchema, tabId: tabIdSchema, });
- src/tools/interaction.ts:19-22 (registration)Registration of the click tool using server.tool with name 'click', description, and clickSchema.shape as input schema.server.tool( 'click', 'Click an element on the page', clickSchema.shape,
- src/tools/interaction.ts:23-56 (handler)The handler function for the click tool: retrieves the page for the tab, waits for the selector to be visible, clicks the element with specified button, count, and delay, handles errors including selector not found.async ({ selector, button, clickCount, delay, timeout, tabId }) => { const pageResult = await getPageForOperation(tabId); if (!pageResult.success) { return handleResult(pageResult); } const page = pageResult.data; const timeoutMs = timeout ?? getDefaultTimeout(); try { // Wait for element to be visible const element = await page.waitForSelector(selector, { timeout: timeoutMs, visible: true, }); if (!element) { return handleResult(err(selectorNotFound(selector))); } await element.click({ button: (button ?? 'left') as MouseButton, clickCount: clickCount ?? 1, delay: delay, }); return handleResult(ok({ clicked: true, selector })); } catch (error) { if (error instanceof Error && error.message.includes('waiting for selector')) { return handleResult(err(selectorNotFound(selector))); } return handleResult(err(normalizeError(error))); } }