Selenium + Python · Lesson 29 of 43

0/43 done

Module 29: File Handling

29. File Uploads via send_keys & Download Verification

15 minlesson

Your task

Implement upload_file(element, file_path_str):

  1. element.send_keys(file_path_str)

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

File Upload in Python Selenium

python
from pathlib import Path

# Send absolute path to <input type='file'>:
file_path = Path("test_data/invoice.pdf").resolve()
driver.find_element(By.CSS_SELECTOR, "input[type='file']").send_keys(str(file_path))

# Verifying downloaded file exists:
download_file = Path.home() / "Downloads" / "report.csv"
assert download_file.exists(), f"Downloaded file {download_file} not found!"

Key Takeaways & Walkthrough Summary

Uploading attachments and checking downloaded spreadsheets are key test scenarios. In this lesson, you will learn why you must use send_keys on file input elements, and how to verify downloaded files with pathlib. Let's handle files!

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

Common Doubts & Lesson Q&A

Can Selenium interact with the native Windows File Picker dialog?

No. WebDriver can only interact with browser DOM elements. Never click the browse button; send the file path directly to the input element.

Ready? Solve the task in the editor

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

Python
Skip