browser_get_html
Extract HTML content from a webpage or specific element using Playwright MCP Server. Simplify web automation by retrieving structured data for analysis or interaction.
Instructions
Extract HTML content from the page or a specific element
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| selector | No |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"selector": {
"type": "string"
}
},
"type": "object"
}
Implementation Reference
- src/server.ts:119-151 (handler)The async handler function that extracts and returns the HTML content from the current page or a specific selector using Playwright's page.content() or element.innerHTML().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/types.ts:23-25 (schema)Zod schema defining the input for browser_get_html: optional selector string.export const BrowserGetHtmlInputSchema = z.object({ selector: z.string().optional() });
- src/server.ts:110-152 (registration)Registration of the browser_get_html tool with McpServer.registerTool, providing title, description, inline inputSchema matching types.ts schema, and inline 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() } }, 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 }; } } );