Automate file upload via a hidden file input element in Playwright

domain: playwright.dev · 5 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Locate the file input element with page.locator('input[type=file]'); even if the input is hidden or styled with display:none, Playwright can interact with it directly
  2. Call await fileInput.setInputFiles('/absolute/path/to/file.pdf') to assign one or more files to the input; Playwright handles hidden inputs without needing to make them visible
  3. To upload multiple files at once, pass an array: fileInput.setInputFiles(['/path/a.png', '/path/b.png'])
  4. For drag-and-drop upload zones that do not use a file input, use page.dispatchEvent() to fire a drop event with a DataTransfer object containing a File object constructed in page.evaluate()
  5. Assert that the upload succeeded by waiting for a success indicator: await expect(page.getByText('Upload complete')).toBeVisible()

Known gotchas

Related routes

Automate interactions inside nested iframes and open shadow DOM in Playwright
playwright.dev · 5 steps · unrated
Handle file downloads and uploads in Playwright tests
playwright.dev · 5 steps · unrated
Configure the Microsoft Playwright MCP server to expose browser automation tools to an LLM agent
github.com/microsoft/playwright-mcp · 6 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans