Playwright + Python · Lesson 23 of 37

0/37 done

Module 23: File Handling

23. Automated File Uploads & Download Verification

15 minlesson

Your task

Implement upload_document(page, selector, file_path):

  1. page.locator(selector).set_input_files(file_path)

Write it in the editor, then press Check answer (Ctrl + Enter).

File Upload & Download in Python

python
from pathlib import Path

page.locator("input[type='file']").set_input_files("test_data/report.pdf")

with page.expect_download() as download_info:
    page.get_by_role("button", name="Export CSV").click()

download = download_info.value
download.save_as(Path("downloads") / download.suggested_filename)

Key Takeaways & Walkthrough Summary

Uploading avatars and verifying downloaded reports are essential workflows. In this lesson, you will master set_input_files and expect_download() in Python. Let's handle files!

Part of the Playwright + Python course (Playwright with Python). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Can I upload multiple files simultaneously?

Yes! Pass a list of file paths: set_input_files(['file1.png', 'file2.png']).

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Python
Skip