getElementContent
Extract HTML and text content from web elements using CSS selectors for web scraping and automation tasks.
Instructions
Get the HTML and text content of a specific element
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| selector | Yes |
Implementation Reference
- src/controllers/playwright.ts:465-487 (handler)The core handler function in PlaywrightController that retrieves the HTML and text content of an element using document.querySelector and returns it as an object.async getElementContent(selector: string): Promise<{html: string, text: string}> { try { if (!this.isInitialized()) { throw new Error('Browser not initialized'); } this.log('Getting element content for selector:', selector); const content = await this.state.page?.evaluate((sel) => { const element = document.querySelector(sel); if (!element) { throw new Error(`Element not found: ${sel}`); } return { html: element.innerHTML, text: element.textContent || '' }; }, selector); this.log('Element content retrieved'); return content || {html: '', text: ''}; } catch (error: any) { console.error('Get element content error:', error); throw new BrowserError('Failed to get element content', 'Check if the element exists'); } }
- src/server.ts:183-193 (schema)Tool schema definition specifying the input as an object with a required 'selector' string property.const GET_ELEMENT_CONTENT_TOOL: Tool = { name: "getElementContent", description: "Get the HTML and text content of a specific element", inputSchema: { type: "object", properties: { selector: { type: "string" } }, required: ["selector"] } };
- src/server.ts:532-532 (registration)Registration of the tool in the tools object passed to the MCP server capabilities.getElementContent: GET_ELEMENT_CONTENT_TOOL,
- src/server.ts:747-758 (handler)MCP server request handler case that validates input, calls the controller handler, and returns the result as JSON.case 'getElementContent': { if (!args.selector) { return { content: [{ type: "text", text: "Selector is required" }], isError: true }; } const content = await playwrightController.getElementContent(args.selector as string); return { content: [{ type: "text", text: JSON.stringify(content, null, 2) }] }; }