Manuals / Playwright with Python / Chapter 16

Part 2 · Core Interactions · intermediate · ~35 min · Chapter 16 of 61

10. File Uploads & Downloads

# Single file page.get_by_label("Upload resume").set_input_files("resume.pdf") # Multiple files page.get_by_label("Attach files").set_input_files(["file1.png", "file2.png"]) # Clear a selected file page.get_by_label("Upload resume").set_input_files([]) This directly sets file(s) on an <input type="file"> element without touching the OS-level native file picker dialog at all. .set_input_files(paths

Path progress
25%

Step 1 of 2

Overview

This directly sets file(s) on an <input type="file"> element without touching the OS-level native file picker dialog at all. .set_input_files(paths) What it does: Sets file(s) on a file input element directly, bypassing the native OS file picker dialog. Types/params: Pointers: Works even on hidden file inputs (a styled "Upload" button triggering a hidden <input type="file">) — no OS-level automation needed. download = download_info.value As with new-tab handling, the with ... expect_download() pattern registers the listener before the triggering click.

  • paths — one of:
  • A single string path → uploads one file
  • A list of string paths → uploads multiple files (if the input supports multiple)
  • An empty list [] → clears the current selection
OverviewDrag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Page under testdrag · tap →Multi-browserdrag · tap →Automation pathdrag · tap →Tooling nodedrag · tap →

Try it

Run / study this snippet

# Single file
Was this step clear?
Chapter learning outcomes
  • # Single file page.get_by_label("Upload resume").set_input_files("resume.pdf") #
  • .set_input_files(paths

Clear these before you leave

Side quest

10. File Uploads & Downloads deliverable

Apply one idea from “Overview” in a small script or note.