Skip to main content
Glama
andytango
by andytango

wait_for_navigation

Wait for page navigation to complete before proceeding with browser automation tasks. Specify timeout and wait conditions for reliable script execution.

Instructions

Wait for the page to navigate to a new URL

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
waitUntilNo
timeoutNoTimeout in milliseconds
tabIdNoTab ID to operate on (uses active tab if not specified)

Implementation Reference

  • The handler function that executes the wait_for_navigation tool. It retrieves the page, waits for navigation with specified options, and returns the new URL and title or handles errors like timeout.
    async ({ waitUntil, timeout, tabId }) => {
      const pageResult = await getPageForOperation(tabId);
      if (!pageResult.success) {
        return handleResult(pageResult);
      }
    
      const page = pageResult.data;
      const timeoutMs = timeout ?? getDefaultTimeout();
    
      try {
        await page.waitForNavigation({
          waitUntil: (waitUntil ?? 'load') as WaitUntilOption,
          timeout: timeoutMs,
        });
    
        return handleResult(ok({
          url: page.url(),
          title: await page.title(),
        }));
      } catch (error) {
        if (error instanceof Error && error.message.includes('timeout')) {
          return handleResult(err(operationTimeout('wait_for_navigation', timeoutMs)));
        }
        return handleResult(err(normalizeError(error)));
      }
    }
  • Zod schema defining the input parameters for the wait_for_navigation tool: waitUntil, timeout (optional), tabId (optional).
    export const waitForNavigationSchema = z.object({
      waitUntil: waitUntilSchema,
      timeout: timeoutSchema,
      tabId: tabIdSchema,
    });
  • Registers the wait_for_navigation tool on the MCP server with description, schema, and handler function.
    server.tool(
      'wait_for_navigation',
      'Wait for the page to navigate to a new URL',
      waitForNavigationSchema.shape,
      async ({ waitUntil, timeout, tabId }) => {
        const pageResult = await getPageForOperation(tabId);
        if (!pageResult.success) {
          return handleResult(pageResult);
        }
    
        const page = pageResult.data;
        const timeoutMs = timeout ?? getDefaultTimeout();
    
        try {
          await page.waitForNavigation({
            waitUntil: (waitUntil ?? 'load') as WaitUntilOption,
            timeout: timeoutMs,
          });
    
          return handleResult(ok({
            url: page.url(),
            title: await page.title(),
          }));
        } catch (error) {
          if (error instanceof Error && error.message.includes('timeout')) {
            return handleResult(err(operationTimeout('wait_for_navigation', 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