playwright_press_key
Simulate keyboard key presses in a browser environment using Playwright. Specify the key to press and an optional CSS selector to focus on an element first, enabling precise automation of web interactions.
Instructions
Press a keyboard key
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | Key to press (e.g. 'Enter', 'ArrowDown', 'a') | |
| selector | No | Optional CSS selector to focus before pressing key |
Implementation Reference
- src/tools/browser/interaction.ts:219-234 (handler)The PressKeyTool.execute method implements the core logic for the playwright_press_key tool: optionally focuses on a selector and presses the specified key using Playwright's page.keyboard.press.export class PressKeyTool extends BrowserToolBase { /** * Execute the key press tool */ async execute(args: any, context: ToolContext): Promise<ToolResponse> { return this.safeExecute(context, async (page) => { if (args.selector) { await page.waitForSelector(args.selector); await page.focus(args.selector); } await page.keyboard.press(args.key); return createSuccessResponse(`Pressed key: ${args.key}`); }); } }
- src/tools.ts:399-410 (schema)Defines the input schema and metadata for the playwright_press_key tool in createToolDefinitions().{ name: "playwright_press_key", description: "Press a keyboard key", inputSchema: { type: "object", properties: { key: { type: "string", description: "Key to press (e.g. 'Enter', 'ArrowDown', 'a')" }, selector: { type: "string", description: "Optional CSS selector to focus before pressing key" } }, required: ["key"], }, },
- src/toolHandler.ts:546-547 (registration)Registers the handler dispatch in the main tool switch statement, calling PressKeyTool.execute.case "playwright_press_key": return await pressKeyTool.execute(args, context);
- src/toolHandler.ts:346-410 (registration)Instantiates the PressKeyTool instance for use in handling the tool call.if (!pressKeyTool) pressKeyTool = new PressKeyTool(server); if (!saveAsPdfTool) saveAsPdfTool = new SaveAsPdfTool(server); if (!clickAndSwitchTabTool) clickAndSwitchTabTool = new ClickAndSwitchTabTool(server); } /** * Main handler for tool calls */ export async function handleToolCall( name: string, args: any, server: any ): Promise<CallToolResult> { // Initialize tools initializeTools(server); try { // Handle codegen tools switch (name) { case 'start_codegen_session': return await handleCodegenResult(startCodegenSession.handler(args)); case 'end_codegen_session': return await handleCodegenResult(endCodegenSession.handler(args)); case 'get_codegen_session': return await handleCodegenResult(getCodegenSession.handler(args)); case 'clear_codegen_session': return await handleCodegenResult(clearCodegenSession.handler(args)); } // Record tool action if there's an active session const recorder = ActionRecorder.getInstance(); const activeSession = recorder.getActiveSession(); if (activeSession && name !== 'playwright_close') { recorder.recordAction(name, args); } // Special case for browser close to ensure it always works if (name === "playwright_close") { if (browser) { try { if (browser.isConnected()) { await browser.close().catch(e => console.error("Error closing browser:", e)); } } catch (error) { console.error("Error during browser close in handler:", error); } finally { resetBrowserState(); } return { content: [{ type: "text", text: "Browser closed successfully", }], isError: false, }; } return { content: [{ type: "text", text: "No browser instance to close", }], isError: false, }; }
- src/tools.ts:470-470 (registration)Includes the tool name in BROWSER_TOOLS array for conditional browser launching."playwright_press_key",