screenshot_by_uid
Capture screenshots of specific webpage elements by their unique identifier. Use this tool to take base64 PNG images of individual elements for testing, debugging, or documentation purposes.
Instructions
Capture element screenshot by UID as base64 PNG.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| uid | Yes | Element UID from snapshot |
Implementation Reference
- src/tools/screenshot.ts:68-105 (handler)MCP tool handler: validates uid arg, gets Firefox instance, calls takeScreenshotByUid, handles special errors for stale UIDs, builds response with size infoexport async function handleScreenshotByUid(args: unknown): Promise<McpToolResponse> { try { const { uid } = args as { uid: string }; if (!uid || typeof uid !== 'string') { throw new Error('uid required'); } const { getFirefox } = await import('../index.js'); const firefox = await getFirefox(); try { const base64Png = await firefox.takeScreenshotByUid(uid); if (!base64Png || typeof base64Png !== 'string') { throw new Error('Invalid screenshot data'); } return buildScreenshotResponse(base64Png, uid); } catch (error) { const errorMsg = (error as Error).message; // Concise error for stale UIDs if ( errorMsg.includes('stale') || errorMsg.includes('Snapshot') || errorMsg.includes('UID') || errorMsg.includes('not found') ) { throw new Error(`${uid} stale/invalid. Call take_snapshot first.`); } throw error; } } catch (error) { return errorResponse(error as Error); } }
- src/tools/screenshot.ts:18-31 (schema)Tool schema definition: specifies name, description, and inputSchema requiring 'uid' stringexport const screenshotByUidTool = { name: 'screenshot_by_uid', description: 'Capture element screenshot by UID as base64 PNG.', inputSchema: { type: 'object', properties: { uid: { type: 'string', description: 'Element UID from snapshot', }, }, required: ['uid'], }, };
- src/index.ts:140-140 (registration)Registers the handler function in the toolHandlers Map used by the MCP server['screenshot_by_uid', tools.handleScreenshotByUid],
- src/index.ts:184-184 (registration)Includes the tool definition in the allTools array returned by listToolstools.screenshotByUidTool,
- src/firefox/dom.ts:315-336 (helper)Core helper: resolves UID to WebElement, scrolls into view, takes element screenshot using Selenium WebElement.takeScreenshot()async takeScreenshotByUid(uid: string): Promise<string> { if (!this.resolveUid) { throw new Error( 'takeScreenshotByUid: resolveUid callback not set. Ensure snapshot is initialized.' ); } const el = await this.resolveUid(uid); // Scroll element into view await this.driver.executeScript( 'arguments[0].scrollIntoView({block: "center", inline: "center"});', el ); // Wait for scroll to complete await new Promise((resolve) => setTimeout(resolve, 100)); // Take screenshot of element (Selenium automatically crops to element bounds) return await el.takeScreenshot(); } }