Skip to main content
Glama

scroll_to_element

Scrolls automatically to locate and display specific UI elements on Android devices using criteria like resource ID, text, or content description.

Instructions

Scroll down repeatedly until an element matching the given criteria is visible

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
byYesHow to find the element
valueYesValue to match
max_scrollsNoMaximum scroll attempts (default 10)
device_idNoDevice ID (optional if only one device)

Implementation Reference

  • The handler and registration for 'scroll_to_element' tool. It checks the UI tree for the element using the provided criteria and performs scrolls if not found.
    server.tool(
      "scroll_to_element",
      "Scroll down repeatedly until an element matching the given criteria is visible",
      {
        by: z.enum(["resource-id", "text", "content-desc"]).describe("How to find the element"),
        value: z.string().describe("Value to match"),
        max_scrolls: z.number().optional().default(10).describe("Maximum scroll attempts (default 10)"),
        device_id: z.string().optional().describe("Device ID (optional if only one device)"),
      },
      async ({ by, value, max_scrolls, device_id }) => {
        const finder: Record<string, (el: { resourceId: string; text: string; contentDesc: string }) => boolean> = {
          "resource-id": (el) => el.resourceId === value || el.resourceId.endsWith(`:id/${value}`),
          text: (el) => el.text.toLowerCase().includes(value.toLowerCase()),
          "content-desc": (el) => el.contentDesc.toLowerCase().includes(value.toLowerCase()),
        };
    
        const info = await adb.getDeviceInfo(device_id);
        const [w, h] = info.screenSize.split("x").map(Number);
        const centerX = Math.round(w / 2);
        const fromY = Math.round(h * 0.7);
        const toY = Math.round(h * 0.3);
    
        for (let i = 0; i < max_scrolls; i++) {
          const elements = await adb.getUiTree(device_id);
          const found = elements.find(finder[by]);
          if (found) {
            return {
              content: [
                {
                  type: "text",
                  text: `Found "${value}" after ${i} scrolls at (${found.center.x},${found.center.y})`,
                },
              ],
            };
          }
          await adb.swipe(centerX, fromY, centerX, toY, 500, device_id);
          await new Promise((r) => setTimeout(r, 500));
        }
    
        return {
          content: [
            { type: "text", text: `Element "${by}=${value}" not found after ${max_scrolls} scrolls` },
          ],
          isError: true,
        };
      },
    );

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/martingeidobler/android-mcp-server'

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