Skip to main content
Glama

page_getElement

Retrieve specific elements from WeChat Mini Program pages using CSS selectors to inspect and manipulate page components during development.

Instructions

通过选择器获取页面元素。

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
connectionNo
selectorYes
innerSelectorNo

Implementation Reference

  • The execute handler for the 'page_getElement' tool. Parses input parameters, acquires the page instance via manager.withPage, resolves the target element using the provided selector (and optional innerSelector), summarizes the element's properties, formats the summary as JSON, and returns it as a text content result.
    execute: async (rawArgs, context: ToolContext) => { const args = getElementParameters.parse(rawArgs ?? {}); return manager.withPage<ContentResult>( context.log, { overrides: args.connection }, async (page) => { const element = await resolveElement( page, args.selector, args.innerSelector ); const summary = await summarizeElement(element); return toTextResult(formatJson(summary)); } ); },
  • Zod schema defining the input parameters for the 'page_getElement' tool: selector (required string) and optional innerSelector, extending the common connection container schema.
    const getElementParameters = connectionContainerSchema.extend({ selector: z.string().trim().min(1), innerSelector: z.string().trim().min(1).optional(), });
  • Factory function that creates and registers the 'page_getElement' tool (via createGetElementTool) among other page-related tools, returning an array of AnyTool objects.
    export function createPageTools(manager: WeappAutomatorManager): AnyTool[] { return [ createGetElementTool(manager), createWaitForElementTool(manager), createWaitForTimeoutTool(manager), createGetPageDataTool(manager), createSetPageDataTool(manager), createCallPageMethodTool(manager), ]; }
  • Helper function used by the handler to resolve and return a page element by selector, supporting optional nested innerSelector queries.
    export async function resolveElement( page: unknown, selector: string, innerSelector?: string ): Promise<any> { if (!page || typeof (page as { $?: unknown }).$ !== "function") { throw new UserError("Page instance is not available to resolve elements."); } let element = await (page as { $: (s: string) => Promise<any> }).$(selector); if (!element) { throw new UserError(`Element not found for selector "${selector}".`); } if (innerSelector) { if (typeof element.$ !== "function") { throw new UserError( `Element for selector "${selector}" does not support nested queries.` ); } const inner = await element.$(innerSelector); if (!inner) { throw new UserError( `Element not found for selector "${innerSelector}" within "${selector}".` ); } element = inner; } return element; }
  • Helper function used by the handler to extract and serialize key properties (tagName, text, value, outerWxml) from the resolved element.
    export async function summarizeElement( element: any ): Promise<Record<string, SerializableValue>> { const tagName = typeof element?.tagName === "string" ? element.tagName : null; const [text, value, outerWxml] = await Promise.all([ typeof element?.text === "function" ? element.text().catch(() => null) : null, typeof element?.value === "function" ? element.value().catch(() => null) : null, typeof element?.outerWxml === "function" ? element.outerWxml().catch(() => null) : null, ]); return { tagName: toSerializableValue(tagName), text: toSerializableValue(text), value: toSerializableValue(value), outerWxml: toSerializableValue(outerWxml), }; }

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/yfmeii/weapp-dev-mcp'

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