Skip to main content
Glama
andytango
by andytango

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)));
        }
      }
    );
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden but lacks behavioral details. It doesn't mention what happens on timeout, whether it blocks execution, error conditions, or interaction with page state. This is inadequate for a tool that likely involves asynchronous operations.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, clear sentence with no wasted words. It's front-loaded and efficiently communicates the core functionality without unnecessary elaboration.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a tool with 5 parameters, no annotations, and no output schema, the description is insufficient. It doesn't explain return values, error behavior, or interaction with other tools like 'query_selector'. The agent lacks context about what 'appear in the page' means operationally.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so parameters are well-documented in the schema. The description adds no additional semantic context beyond implying the tool operates on a page, which is minimal value. Baseline 3 is appropriate as the schema handles parameter documentation.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('wait for') and resource ('element matching the selector'), making the purpose understandable. However, it doesn't differentiate from sibling tools like 'wait' or 'wait_for_navigation', which could cause confusion about when to choose this specific waiting tool.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance is provided on when to use this tool versus alternatives like 'wait' or 'wait_for_navigation'. The description only states what it does without context about appropriate scenarios or prerequisites, leaving the agent to infer usage.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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