Skip to main content
Glama

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
NameRequiredDescriptionDefault
selectorYesCSS selector for the element
timeoutNoTimeout in milliseconds
tabIdNoTab ID to operate on (uses active tab if not specified)

Implementation Reference

  • 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))); } }
  • 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, });
  • 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,

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/andytango/puppeteer-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server