focus
Focus on a web page element using a CSS selector to enable user interactions like typing or clicking within that element.
Instructions
Focus an element on the page
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| selector | Yes | CSS selector for the element | |
| timeout | No | Timeout in milliseconds | |
| tabId | No | Tab ID to operate on (uses active tab if not specified) |
Implementation Reference
- src/tools/interaction.ts:168-195 (handler)The handler function for the 'focus' tool. It retrieves the page for the given tab, waits for the element matching the selector to appear, calls focus() on it using Puppeteer ElementHandle, and returns a success result or appropriate error.async ({ selector, timeout, tabId }) => { const pageResult = await getPageForOperation(tabId); if (!pageResult.success) { return handleResult(pageResult); } const page = pageResult.data; const timeoutMs = timeout ?? getDefaultTimeout(); try { const element = await page.waitForSelector(selector, { timeout: timeoutMs, }); if (!element) { return handleResult(err(selectorNotFound(selector))); } await element.focus(); return handleResult(ok({ focused: true, selector })); } catch (error) { if (error instanceof Error && error.message.includes('waiting for selector')) { return handleResult(err(selectorNotFound(selector))); } return handleResult(err(normalizeError(error))); } }
- src/schemas.ts:81-85 (schema)Zod schema defining the input parameters for the 'focus' tool: CSS selector (required), timeout (optional ms), tabId (optional).export const focusSchema = z.object({ selector: selectorSchema, timeout: timeoutSchema, tabId: tabIdSchema, });
- src/tools/interaction.ts:164-167 (registration)Registration of the 'focus' tool via server.tool() call within registerInteractionTools function, specifying name, description, input schema shape, and handler.server.tool( 'focus', 'Focus an element on the page', focusSchema.shape,