Selenium + Python · Lesson 29 of 43
Module 29: File Handling
Your task
Implement upload_file(element, file_path_str):
Write it in the editor, then press Check answer (Ctrl + Enter).
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!"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.
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.
___