Skip to main content
Glama

browser_focus_element

Focus on a specific web element using locator strategies like ID, CSS, or XPath to interact with page elements during browser automation.

Instructions

Focus on a specific element

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
byYesLocator strategy to find element
valueYesValue for the locator strategy
timeoutNoMaximum time to wait for element in milliseconds

Implementation Reference

  • Registration of the 'browser_focus_element' tool, including inline handler function that creates ActionService and calls focusElement
    server.tool('browser_focus_element', 'Focus on a specific element', { ...locatorSchema }, async ({ by, value }) => {
      try {
        const driver = stateManager.getDriver();
        const actionService = new ActionService(driver);
        await actionService.focusElement({ by, value });
        return {
          content: [{ type: 'text', text: `Focused on element` }],
        };
      } catch (e) {
        return {
          content: [
            {
              type: 'text',
              text: `Error focusing on element: ${(e as Error).message}`,
            },
          ],
        };
      }
    });
  • Inline handler function for the tool, which handles execution, error catching, and response formatting
    server.tool('browser_focus_element', 'Focus on a specific element', { ...locatorSchema }, async ({ by, value }) => {
      try {
        const driver = stateManager.getDriver();
        const actionService = new ActionService(driver);
        await actionService.focusElement({ by, value });
        return {
          content: [{ type: 'text', text: `Focused on element` }],
        };
      } catch (e) {
        return {
          content: [
            {
              type: 'text',
              text: `Error focusing on element: ${(e as Error).message}`,
            },
          ],
        };
      }
    });
  • Zod schema for locator parameters used in the tool's input schema
    export const locatorSchema = {
      by: z
        .enum(['id', 'css', 'xpath', 'name', 'tag', 'class', 'link', 'partialLink'])
        .describe('Locator strategy to find element'),
      value: z.string().describe('Value for the locator strategy'),
      timeout: z.number().optional().describe('Maximum time to wait for element in milliseconds'),
    };
  • Core implementation of focusing an element using Selenium WebDriver's executeScript to call focus() on the located element
    async focusElement(params: LocatorParams): Promise<void> {
      const locator = LocatorFactory.createLocator(params.by, params.value);
      const element = await this.driver.wait(until.elementLocated(locator), params.timeout || 15000);
      await this.driver.executeScript('arguments[0].focus();', element);
    }

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/pshivapr/selenium-mcp'

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