get_text
Extract text content from web page elements using CSS selectors for browser automation with Playwright MCP.
Instructions
Get text content from an element
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"selector": "<selector>"
},
"name": "get_text"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| selector | Yes | ||
| page_id | No |
Implementation Reference
- src/playwright_mcp/server.py:256-264 (handler)The main handler for the 'get_text' tool within the tool dispatcher. It retrieves the text content from the specified selector on the active Playwright page.
elif name == "get_text": selector = arguments.get("selector") if not selector: raise ValueError("Selector is required") page = get_active_page(arguments.get("page_id")) text = await page.text_content(selector) return [types.TextContent(type="text", text=text or "")] - src/playwright_mcp/server.py:122-133 (registration)Registers the 'get_text' tool in the list_tools handler, including its name, description, and input schema.
types.Tool( name="get_text", description="Get text content from an element", inputSchema={ "type": "object", "properties": { "selector": {"type": "string"}, "page_id": {"type": "string"}, }, "required": ["selector"], }, ), - src/playwright_mcp/server.py:125-132 (schema)Defines the input schema for the 'get_text' tool, requiring a 'selector' and optionally 'page_id'.
inputSchema={ "type": "object", "properties": { "selector": {"type": "string"}, "page_id": {"type": "string"}, }, "required": ["selector"], }, - src/playwright_mcp/server.py:200-211 (helper)Helper function used by the get_text handler to resolve the Playwright Page instance from page_id or default.
def get_active_page(page_id: Optional[str] = None) -> Page: """Get the active page based on page_id or current default.""" global current_page_id if page_id is None: page_id = current_page_id if page_id not in pages: raise ValueError(f"Page not found: {page_id}") return pages[page_id]