DEV Community

Cover image for How to Upload Files with Selenium

Posted on • Updated on

How to Upload Files with Selenium

Anyone who is using Selenium eventually reaches a point where they need to perform a file upload.

We'll be using the FileBit site as an example.

I'll also be making some comparisons between Selenium and Endtest:

First, let's see how a file upload usually takes place.

1. You click on the Upload button.

2. A window from the OS opens which allows you to select the file.

On Windows:

On macOS:

After you select the file, the local path of the file is written in the file input.

Every upload form on any site has a file input, you can easily find it by looking for an input element that has the type="file" attribute.

And the file upload is completed:

You might think this will be straightforward:

1. Ask Selenium to click on the Upload button.

2. Ask Selenium to click on the file that we want.

But that won't work.


Because Selenium can only interact with elements from inside the DOM.
The window from the Operating System is not part of the DOM.

The worst part?

Your test will get stuck because Selenium can't close that window.

The solution is to write the local absolute path of the file directly in file input, without even having to click on the Upload button.

file_input = driver.find_element_by_id("fileField")

In case you're using Python and you're running your tests on multiple machines, you can easily get the absolute path of a file from the relative path:

import os
absolute_file_path = os.path.abspath("files/Sales_April_2019.xlsx")
file_input = driver.find_element_by_id("fileField")

Once the site detects that a file path has been written in the file input, it will automatically upload that file.

One inconvenience is that most sites tend to hide that file input and Selenium cannot find an element which is hidden.

The solution?

Execute some JavaScript code that will change the CSS for that file input and make it visible.

If your site is using jQuery:


If your site isn't using jQuery, just use vanilla JavaScript:


Remember that you need to execute that JavaScript code before the file upload.

Selenium offers an easy way to execute JavaScript:

change_visibility = '$("#fileField").css("visibility,"visible");'
change_display = '$("#fileField").css("display,"block");'

That usually does the trick.

But developers have different ways of hiding the file input.

Some might give it a width and height of zero pixels, some might move it outside of the visible area.

You need to figure out what they did to hide it and undo it.

Remember, Selenium cannot interact with an element in these cases:

  • element is not visible
  • element does not have a width and a height
  • element is covered by another element

If you're struggling and can't figure it out, just execute this entire block:


Uploading a file in a test is easier with Endtest than it is with Selenium:

1. You first need to upload your file in the Drive section:

Make sure to copy the File Download URL.

2. Use the Upload File action in your test:

3. Execute the test on their cross-browser cloud:

Top comments (3)

tdelmonte profile image

Excellent article, good graphics, enough details and easy to follow.

alexdino profile image

thanks for sharing this please how i can upload a file from a url for example my file not locally but hosted in google drive or mega

taolangao profile image
Chu Van Vu

Thanks bro!