Skip to main content
Glama

wait_for_element

Wait for dynamic page elements to load before interacting with them, preventing errors when handling asynchronous content or page transitions.

Instructions

Wait for a specific element to appear on the page before continuing. Essential for handling dynamic content that loads asynchronously, page transitions, or elements that appear after clicking buttons. Prevents errors from trying to interact with elements that haven't loaded yet. Commonly used after login, navigation, or clicking buttons that trigger loading states.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sessionIdYesSession ID obtained from initialize_session
selectorYesCSS selector of the element to wait for (e.g., '.chat-container', '#message-input', '[data-loaded="true"]')
timeoutNoMaximum time in milliseconds to wait before timing out. Increase for slow-loading pages (default: 30000)

Implementation Reference

  • Core handler function that executes the wait_for_element tool logic using Playwright to wait for the specified selector.
    export async function waitForElement(sessionId, selector, timeout = 30000) {
      const session = getSession(sessionId);
      const { page } = session;
    
      try {
        await page.waitForSelector(selector, { timeout });
    
        return {
          success: true,
          sessionId,
          selector,
          message: `Element "${selector}" found`,
          currentUrl: page.url(),
        };
      } catch (error) {
        throw new Error(`Element "${selector}" not found within ${timeout}ms`);
      }
    }
  • Input schema definition for the wait_for_element tool, specifying parameters and validation.
      type: "object",
      properties: {
        sessionId: {
          type: "string",
          description: "Session ID obtained from initialize_session",
        },
        selector: {
          type: "string",
          description:
            "CSS selector of the element to wait for (e.g., '.chat-container', '#message-input', '[data-loaded=\"true\"]')",
        },
        timeout: {
          type: "number",
          description:
            "Maximum time in milliseconds to wait before timing out. Increase for slow-loading pages (default: 30000)",
          default: 30000,
        },
      },
      required: ["sessionId", "selector"],
    },
  • src/index.js:247-272 (registration)
    Tool registration in the MCP server's ListTools response, defining name, description, and schema.
    {
      name: "wait_for_element",
      description:
        "Wait for a specific element to appear on the page before continuing. Essential for handling dynamic content that loads asynchronously, page transitions, or elements that appear after clicking buttons. Prevents errors from trying to interact with elements that haven't loaded yet. Commonly used after login, navigation, or clicking buttons that trigger loading states.",
      inputSchema: {
        type: "object",
        properties: {
          sessionId: {
            type: "string",
            description: "Session ID obtained from initialize_session",
          },
          selector: {
            type: "string",
            description:
              "CSS selector of the element to wait for (e.g., '.chat-container', '#message-input', '[data-loaded=\"true\"]')",
          },
          timeout: {
            type: "number",
            description:
              "Maximum time in milliseconds to wait before timing out. Increase for slow-loading pages (default: 30000)",
            default: 30000,
          },
        },
        required: ["sessionId", "selector"],
      },
    },
  • src/index.js:513-529 (registration)
    Dispatch handler in CallToolRequestSchema that validates parameters and calls the waitForElement function.
    case "wait_for_element": {
      const { sessionId, selector, timeout = 30000 } = args;
      if (!sessionId) {
        throw new McpError(
          ErrorCode.InvalidParams,
          "sessionId parameter is required"
        );
      }
      if (!selector) {
        throw new McpError(
          ErrorCode.InvalidParams,
          "selector parameter is required"
        );
      }
      result = await waitForElement(sessionId, selector, timeout);
      break;
    }
  • Re-export of waitForElement function from interaction.js to centralize imports.
    export { clickElement, fillForm, waitForElement } from "./interaction.js";
Behavior3/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It explains the tool's purpose (waiting for elements) and common use cases, but lacks details on what happens on timeout (e.g., throws error, returns null), whether it polls continuously, or if it affects page state. It adds some context about preventing errors, but doesn't fully describe the tool's behavior under different conditions.

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

Conciseness4/5

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

The description is appropriately sized and front-loaded, starting with the core purpose. Each sentence adds value: explaining why it's essential, what it prevents, and when to use it. While efficient, it could be slightly more concise by combining some of the use case examples into a single phrase.

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

Completeness3/5

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

For a tool with 3 parameters, 100% schema coverage, but no annotations or output schema, the description is adequate but has gaps. It covers purpose and usage well, but lacks behavioral details (e.g., timeout behavior, return values) that would be important for an agent to use it correctly. The context is complete enough for basic understanding but not for robust implementation.

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 the schema already documents all three parameters thoroughly. The description doesn't add any parameter-specific information beyond what's in the schema (e.g., it doesn't explain selector syntax beyond examples already given, or elaborate on timeout implications). Baseline 3 is appropriate when the schema does the heavy lifting.

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

Purpose5/5

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

The description clearly states the tool's purpose with specific verb ('wait for') and resource ('specific element'), distinguishing it from siblings like click_element or navigate_to_url. It explicitly mentions the goal of preventing errors from interacting with unloaded elements, which is distinct from other tools' functions.

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

Usage Guidelines5/5

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

The description provides explicit guidance on when to use this tool: for handling dynamic content, asynchronous loading, page transitions, or elements appearing after clicks. It gives concrete examples like 'after login, navigation, or clicking buttons that trigger loading states,' clearly differentiating it from other tools that perform actions rather than waiting.

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/pyscout/webscout-mcp'

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