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";

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