browser_file_upload
Upload files to web forms by specifying file paths and element locators for automated browser testing and interaction.
Instructions
Uploads a file using a file input element
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| by | Yes | Locator strategy to find element | |
| value | Yes | Value for the locator strategy | |
| timeout | No | Maximum time to wait for element in milliseconds | |
| filePath | Yes | Absolute path to the file to upload |
Implementation Reference
- src/tools/elementTools.ts:359-385 (registration)Registers the browser_file_upload MCP tool, defining its input schema (locator strategy, value, timeout, filePath) and handler function that uses ElementService to perform the upload.server.tool( 'browser_file_upload', 'Uploads a file using a file input element', { ...locatorSchema, filePath: z.string().describe('Absolute path to the file to upload'), }, async ({ by, value, filePath, timeout = 15000 }) => { try { const driver = stateManager.getDriver(); const elementService = new ElementService(driver); await elementService.uploadFile({ by, value, filePath, timeout }); return { content: [{ type: 'text', text: 'File upload initiated' }], }; } catch (e) { return { content: [ { type: 'text', text: `Error uploading file: ${(e as Error).message}`, }, ], }; } } );
- src/services/elementService.ts:45-48 (helper)Core implementation of file upload in ElementService: locates the file input element and sends the absolute file path using Selenium's sendKeys method.async uploadFile(params: LocatorParams & { filePath: string }): Promise<void> { const element = await this.findElement(params); await element.sendKeys(params.filePath); }
- src/types/index.ts:29-35 (schema)locatorSchema defining the input parameters for element location (by, value, timeout), spread into the tool's schema.export const locatorSchema = { by: z .enum(['id', 'css', 'xpath', 'name', 'tag', 'class', 'link', 'partialLink']) .describe('Locator strategy to find element'), value: z.string().describe('Value for the locator strategy'), timeout: z.number().optional().describe('Maximum time to wait for element in milliseconds'), };