Forem

Cover image for Upload File | Selenium Webdriver Python Tutorial
Dilpreet Johal
Dilpreet Johal

Posted on

3 1

Upload File | Selenium Webdriver Python Tutorial

In this post, we will learn how to upload a file using Selenium Webdriver in Python as well as using the SeleniumBase framework. File uploads are common use cases for the majority of websites these days, so it is important to know how to work with them.

Typically, you’ll run into two scenarios when working with file uploads. 1 – where the file input field is visible. 2 – where the file input field is hidden (most common in modern websites). Let’s take a look at an example for both of them –

Upload file on a visible input field

For this example, I will be using this website – https://the-internet.herokuapp.com/upload

visible input

As you can see in the screenshot above, the “Choose File” input field is visible. So, in this case, we can easily find the input field and send the file to that.

visible code


Upload file on a hidden input field

However, most likely, you will see in the modern websites that the file input field is hidden for styling purposes. Every website will have its own kind of implementation to hide the input field, for example – reducing the opacity to 0, setting display to none, etc…

For this example, we will be using this website – https://practice.automationbro.com/cart/

hidden input

As you can see in the screenshot above, we do not see any “Choose File” input field as that is hidden behind the “Select File” button.

Solution: So to overcome this problem, we need to make the element visible again by manipulating the DOM using JavaScript.

hidden code

The key thing to notice here is the add js code part where I am removing the hidden class to enable our input field and then uploading the file.


Check out the video below to learn how to effectively upload file in Selenium Python –


📧 Subscribe to my mailing list to get access to more content like this

👍 Follow automationbro on Twitter for the latest updates

...

I love coffees! And, if this post helped you out and you would like to support my work, you can do that by clicking on the button below and buying me a cup of coffee -

Buy me a coffee

You can also support me by liking and sharing this content.

Thanks for reading!

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay