29. File Uploads via send_keys & Download Verification

File Upload in Python Selenium

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!"

Task

  • Implement upload_file(element, file_path_str):
  • 1. element.send_keys(file_path_str)
πŸ’‘Common doubts
Did you like the problem?
Lesson 29 of 43
Python
Loading code editor…
Next