Skip to main content
Glama

click_element

Click buttons, links, or interactive elements on web pages using CSS selectors or visible text. Automatically waits for page updates after clicking to navigate interfaces, open modals, or trigger UI actions.

Instructions

Click a button, link, or any interactive element on the page. Useful for navigating through multi-step interfaces, opening chat modals, starting new conversations, or triggering UI actions. Can target elements by CSS selector or by their visible text content. Automatically waits after clicking to allow page updates.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sessionIdYesSession ID obtained from initialize_session
selectorNoCSS selector for the element to click (e.g., 'button#start-chat', '.new-conversation-btn'). Use this when you know the exact selector.
textNoAlternative to selector: visible text content to search for and click (e.g., 'Start Chat', 'Sign In', 'New Conversation'). Use this when selector is unknown.
waitAfterNoMilliseconds to wait after clicking to allow animations, redirects, or dynamic content to load (default: 1000)

Implementation Reference

  • Core implementation of the clickElement function. Handles clicking elements by CSS selector or visible text content using Playwright's page.click or evaluate-based text search. Includes error handling and returns success status with current page info.
    export async function clickElement(
      sessionId,
      selector = null,
      text = null,
      waitAfter = 1000
    ) {
      const session = getSession(sessionId);
      const { page } = session;
    
      try {
        if (selector) {
          await page.click(selector);
        } else if (text) {
          // Try to find element by text content
          const clickedByText = await page.evaluate((searchText) => {
            const elements = Array.from(
              document.querySelectorAll('button, a, [role="button"], [onclick]')
            );
            const target = elements.find((el) =>
              el.textContent.trim().toLowerCase().includes(searchText.toLowerCase())
            );
            if (target) {
              target.click();
              return true;
            }
            return false;
          }, text);
    
          if (!clickedByText) {
            // Try using Playwright's text selector
            await page.click(`text=${text}`);
          }
        } else {
          throw new Error("Either selector or text parameter is required");
        }
    
        await page.waitForTimeout(waitAfter);
    
        return {
          success: true,
          sessionId,
          currentUrl: page.url(),
          title: await page.title(),
          message: `Clicked element successfully`,
        };
      } catch (error) {
        throw new Error(`Failed to click element: ${error.message}`);
      }
    }
  • MCP tool schema definition for 'click_element', including name, description, and detailed inputSchema with properties for sessionId, selector, text, waitAfter, and required fields.
      name: "click_element",
      description:
        "Click a button, link, or any interactive element on the page. Useful for navigating through multi-step interfaces, opening chat modals, starting new conversations, or triggering UI actions. Can target elements by CSS selector or by their visible text content. Automatically waits after clicking to allow page updates.",
      inputSchema: {
        type: "object",
        properties: {
          sessionId: {
            type: "string",
            description: "Session ID obtained from initialize_session",
          },
          selector: {
            type: "string",
            description:
              "CSS selector for the element to click (e.g., 'button#start-chat', '.new-conversation-btn'). Use this when you know the exact selector.",
          },
          text: {
            type: "string",
            description:
              "Alternative to selector: visible text content to search for and click (e.g., 'Start Chat', 'Sign In', 'New Conversation'). Use this when selector is unknown.",
          },
          waitAfter: {
            type: "number",
            description:
              "Milliseconds to wait after clicking to allow animations, redirects, or dynamic content to load (default: 1000)",
            default: 1000,
          },
        },
        required: ["sessionId"],
      },
    },
  • src/index.js:441-457 (registration)
    Tool dispatch/registration in the central CallToolRequestSchema handler. Matches 'click_element' tool name, validates parameters, and invokes the clickElement implementation.
    case "click_element": {
      const { sessionId, selector, text, waitAfter = 1000 } = args;
      if (!sessionId) {
        throw new McpError(
          ErrorCode.InvalidParams,
          "sessionId parameter is required"
        );
      }
      if (!selector && !text) {
        throw new McpError(
          ErrorCode.InvalidParams,
          "Either selector or text parameter is required"
        );
      }
      result = await clickElement(sessionId, selector, text, waitAfter);
      break;
    }
Behavior4/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 effectively describes key behaviors: the tool 'Automatically waits after clicking to allow page updates' and can target elements by 'CSS selector or by their visible text content', covering interaction methods and post-action handling, though it lacks details on error handling or permissions.

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 appropriately sized and front-loaded, with the first sentence stating the core purpose, followed by usage examples and behavioral details. Every sentence adds value without redundancy, making it efficient and well-structured.

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

Completeness4/5

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

Given the tool's moderate complexity (4 parameters, no output schema, no annotations), the description is mostly complete, covering purpose, usage, and key behaviors. However, it lacks details on error cases (e.g., what happens if the element is not found) and does not mention the sessionId parameter's role, leaving minor gaps in context.

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 parameters thoroughly. The description adds minimal value beyond the schema by mentioning the two targeting methods (selector and text) and the wait behavior, but does not provide additional syntax or format details, aligning with the baseline for high schema coverage.

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 verbs ('Click a button, link, or any interactive element') and resources ('on the page'), distinguishing it from siblings like fill_form or navigate_to_url by focusing on UI interaction rather than navigation or form input.

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

Usage Guidelines4/5

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

The description provides clear context for when to use the tool ('Useful for navigating through multi-step interfaces, opening chat modals, starting new conversations, or triggering UI actions'), but does not explicitly state when not to use it or name alternatives among siblings, such as wait_for_element for non-click actions.

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