+91 9790664230
kumar.arun211@gmail.com

File upload using Python Selenium Webdriver

Blog to learn python and selenium

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

  1. The file is base64 encoded
  2. 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.

 

One Response

  1. […] Previous: Previous post: Read data from excel in python scriptsNext: Next post: File upload using Python Selenium Webdriver […]

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2018 Copyrights. All Rights Reserved. Arunkumar Velusamy