upload_file
Specify a CSS selector and absolute file path to upload a file to a web page's file input element.
Instructions
Upload a file to an input[type='file'] element on the page
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| selector | Yes | CSS selector for the file input element | |
| filePath | Yes | Absolute path to the file to upload |
Implementation Reference
- The execute method of UploadFileTool that handles file upload logic. It creates a scoped locator, selects the element (erroring on multiple matches), and calls Playwright's setInputFiles to upload the file.
async execute(args: any, context: ToolContext): Promise<ToolResponse> { this.recordInteraction(); return this.safeExecute(context, async (page) => { // Use standard element selection with error on multiple matches const locator = await this.createScopedLocator(page, args.selector); const { element } = await this.selectPreferredLocator(locator, { errorOnMultiple: true, originalSelector: args.selector, }); await element.setInputFiles(args.filePath); return createSuccessResponse(`Uploaded file '${args.filePath}' to '${args.selector}'`); }); } } - Input schema for upload_file tool. Defines required parameters: 'selector' (CSS selector for the file input element) and 'filePath' (absolute path to the file to upload).
static getMetadata(sessionConfig?: SessionConfig): ToolMetadata { return { name: "upload_file", description: "Upload a file to an input[type='file'] element on the page", annotations: ANNOTATIONS.interaction, inputSchema: { type: "object", properties: { selector: { type: "string", description: "CSS selector for the file input element" }, filePath: { type: "string", description: "Absolute path to the file to upload" } }, required: ["selector", "filePath"], }, }; - src/tools/browser/register.ts:69-69 (registration)UploadFileTool is included in the BROWSER_TOOL_CLASSES array which registers it as an available browser tool.
UploadFileTool, - src/tools/browser/register.ts:18-18 (registration)Import statement for UploadFileTool in the browser tool registration file.
import { UploadFileTool } from './interaction/upload_file.js'; - src/tools/browser/base.ts:363-371 (helper)The safeExecute method from BrowserToolBase (parent class) that provides error handling, browser connection checks, and page closure checks wrapping the upload execution logic.
protected async safeExecute( context: ToolContext, operation: (page: Page) => Promise<ToolResponse> ): Promise<ToolResponse> { const pageError = this.validatePageAvailable(context); if (pageError) return pageError; try { // Verify browser is connected before proceeding