browser_get_html
Extract HTML content from web pages or specific elements to retrieve structured data for analysis, testing, or automation workflows.
Instructions
Extract HTML content from the page or a specific element
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| selector | No |
Implementation Reference
- src/server.ts:119-152 (handler)The asynchronous handler function that implements the core logic for the 'browser_get_html' tool. It validates input, ensures Playwright connection, retrieves the page, extracts HTML from the full page or specified selector using page.content() or element.innerHTML(), and returns the HTML or an error response.async (params: any) => { try { const input = z.object({ selector: z.string().optional() }).parse(params); await this.playwright.ensureConnected(); const page = this.playwright.getPage(); let html: string; if (input.selector) { const element = await page.locator(input.selector); html = await element.innerHTML(); } else { html = await page.content(); } return { content: [{ type: 'text', text: html }] }; } catch (error) { return { content: [{ type: 'text', text: `HTML extraction failed: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } );
- src/server.ts:110-118 (registration)The registration call for the 'browser_get_html' tool in the MCP server, specifying the tool name, metadata (title, description), and inline input schema before passing the handler function.this.server.registerTool( 'browser_get_html', { title: 'Get HTML Content', description: 'Extract HTML content from the page or a specific element', inputSchema: { selector: z.string().optional() } },
- src/types.ts:23-25 (schema)Zod input schema definition for the browser_get_html tool, defining optional 'selector' parameter for targeting specific elements.export const BrowserGetHtmlInputSchema = z.object({ selector: z.string().optional() });
- src/types.ts:61-61 (schema)TypeScript type definition inferred from BrowserGetHtmlInputSchema for type-safe input handling.export type BrowserGetHtmlInput = z.infer<typeof BrowserGetHtmlInputSchema>;