File Uploads In Selenium WebDriver

Brian Grogan Jr.
Qualitas Ex Machina
2 min readJun 1, 2020
File Upload Form

Selenium provides some really useful functions for interacting with web browsers. In my own automation projects, I’ve used the browser window management methods frequently, and I’m really thankful they exist.

But the Selenium library does not cover everything in browser automation. One type of browser interaction that is still missing is the file upload, or more precisely, interactions with the file type of <Input> element.

sendKeys to the Rescue

Selenium does not offer any class or method to help with uploading a file. If you try to click the “Choose File” button on a form, the browser displays a native file chooser dialog, which is outside of the control of the browser, and therefore out of Selenium’s automation reach.

One clever trick you can use instead is the sendKeys() method:

Rather than performing a bunch of complex clicking and dragging, you can simply get a reference to the <Input> element of type “file”, and call the sendKeys() method to send an absolute path to the file on your machine (remember to use an absolute path, it’s important).

Once you call sendKeys() with a file path on the <Input> element, you’ll probably see the element fill with the path to your file! Now, you can either click the “Submit” button on the form, or call the submit() method. And that’s it.

Selenium does not have a comprehensive API to easily do everything in a browser. But with some creative thinking, you can almost always find a way.

--

--

Brian Grogan Jr.
Qualitas Ex Machina

Software Quality Engineer fighting for truth, justice, and better quality apps. Thinks a lot about Automation and CI/CD, in both practical and abstract terms.