File upload using Python Selenium Webdriver
We have seen how to download file using Python Selenium Webdriver in this article. File upload is also frequently used use case in automation testing and in this article, lets learn how to upload a file using Python Selenium Webdriver.
We use the same file upload sample page that we used for our Java example here.
Sample HTML:
<!DOCTYPE html> <html> <body> <form action="**"> <input type="file" name="uploadfile" id="uploadfile"/> <input type="button" name="submit" id="submit"/> </form> </body> </html>
Python Selenium Webdriver comes with native file upload feature. Uploading files in WebDriver is done by simply using the send_keys() method on the ‘file select’ input field i.e. just enter the path to the file to be uploaded.
There is no setup required for running tests with file upload scenarios locally as well as in remote, all we need to do is use the sendKeys command to type the local path of the file in the file field. This is because Python’s remote webdriver uses LocalFileDetector() by default.
This is sufficient and works very well in all drivers. Because local file detection is enabled by default in python selenium webdriver, there is no problem in executing the same test in remote server (for example, Selenium Grid running in some other server) and locally.
When tests are running in local, webdriver does nothing special and just uploads file in given path. But when tests are running with remote driver, two things happens
- The file is base64 encoded
- And sent transparently through the JSON Wire Protocol to the remote server where test is running
This is before writing the fixed remote path. This is an straight forward solution which lets users to switch their tests from a local to remote Driver without having to worry about code change(However you need to have strategy to create local as well as remote webdriver based on some conditions and this condition could be from anywhere such as property file, command line, etc…).
This feature is available in all the official Selenium 2 bindings from Selenium 2.8.0 or newer as this feature has been released then. Here are some examples tests:
At any case, Do not click on browse button to upload file. This will open up OS native file explorer and halt the tests since selenium cannot handle those kind of stuffs.
Below is python example code:
from selenium import webdriver driver = webdriver.Firefox() driver.implicitly_wait(15) driver.get("http://example.com/file-upload-page") driver.find_element_by_id("id-of-uploadfile-element").send_keys("file-path") driver.find_element_by_id("submit").click()
This is very simple, isn’t it?
If you need to override the default file upload behavior of Python Selenium Webdriver, you can use one of the available file detectors from selenium.webdriver.remote.file_detector
from selenium.webdriver.remote.file_detector import UselessFileDetector driver = webdriver.Firefox() driver.file_detector = UselessFileDetector()
This sample is for running tests in remote and referring files from local. If you want to send files to one machine/local to remote, you must use other file transfer options available outside of selenium.
Hope this helps you efficiently in your Python Selenium projects. If there are any different approach to work with the file uploads that you know, please use the comment box to share it which will help all.
7 Responses
Hi. I have uploaded file successfully but after that the window is opened for selecting the file that I need but I don’t need that window anymore . How can I do that?
Hi, please help. What to do if I have input like this:
Function “send_keys()” for this inputs raise “ElementNotInteractableException: Message: element not interactable”.
Thank.
I am still got File Not Found Error.
I am using virtual env. It depends?
This worked perfectly! Thank so much for your article.
How to upload in Sauce Labs?
The directory in my local system in not accessible in it.
(Message: invalid argument: File not found )
How to upload when path not available?
driver.find_element_by_id(“id-of-uploadfile-element”).send_keys(“file-path”)
The send_keys(“file_path”) method. Where do you take the “file_path” from before finding it? Or is this a parameter of the send_keys()?
Thank you
“file_path” should be the actual file path you are uploading. For example, if you want to upload a file data.json located in your Downloads folder, file paths could be
C:\Downloads\data.json – in Windows
/user/Downloads/data.json – in Linux/Mac