Skip to main content
Glama

wait_for_selector

Pauses script execution until a specified CSS selector element appears, becomes visible, or disappears in the browser page, ensuring reliable automation timing.

Instructions

Wait for an element matching the selector to appear in the page

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
selectorYesCSS selector for the element
visibleNoWait for element to be visible
hiddenNoWait for element to be hidden
timeoutNoTimeout in milliseconds
tabIdNoTab ID to operate on (uses active tab if not specified)

Implementation Reference

  • Executes the wait_for_selector tool: gets the page, waits for the selector using Puppeteer page.waitForSelector with options, handles not found and timeout errors, returns success with found selector.
    async ({ selector, visible, hidden, 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, visible: visible ?? false, hidden: hidden ?? false, }); if (!element) { return handleResult(err(selectorNotFound(selector))); } return handleResult(ok({ found: true, selector, })); } catch (error) { if (error instanceof Error) { if (error.message.includes('waiting for selector')) { return handleResult(err(selectorNotFound(selector))); } if (error.message.includes('timeout')) { return handleResult(err(operationTimeout('wait_for_selector', timeoutMs))); } } return handleResult(err(normalizeError(error))); } }
  • Zod schema defining the input parameters for the wait_for_selector tool.
    export const waitForSelectorSchema = z.object({ selector: selectorSchema, visible: z.boolean().optional().default(false).describe('Wait for element to be visible'), hidden: z.boolean().optional().default(false).describe('Wait for element to be hidden'), timeout: timeoutSchema, tabId: tabIdSchema, });
  • Registers the wait_for_selector MCP tool with its description, input schema, and handler function.
    server.tool( 'wait_for_selector', 'Wait for an element matching the selector to appear in the page', waitForSelectorSchema.shape, async ({ selector, visible, hidden, 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, visible: visible ?? false, hidden: hidden ?? false, }); if (!element) { return handleResult(err(selectorNotFound(selector))); } return handleResult(ok({ found: true, selector, })); } catch (error) { if (error instanceof Error) { if (error.message.includes('waiting for selector')) { return handleResult(err(selectorNotFound(selector))); } if (error.message.includes('timeout')) { return handleResult(err(operationTimeout('wait_for_selector', timeoutMs))); } } return handleResult(err(normalizeError(error))); } } );

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