SDET Unicorns

Selenium Webdriver Python Upload File

Selenium Python Upload File

Table of Content

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 –

image 3

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.

    def test_visible_upload(self):
        # open page"")

        # get file path
        file_path = './data/logo.jpg'

        # upload file
        self.choose_file("#file-upload", file_path)

        # click the upload button"#file-submit")

        # assert file uploaded text
        self.assert_text("File Uploaded!", "h3")

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, example – reducing opacity to 0, setting display to none etc…

For this example, we will be using this website –

image 4

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.

    def test_hidden_upload(self):
        # open page"")

        # get file path
        file_path = './data/logo.jpg'

        # add js code
        remove_hidden_class = "document.getElementById('upfile_1').classList.remove('file_input_hidden')"

        # upload file
        self.choose_file('#upfile_1', file_path)

        # click the upload button"#upload_1")

        # assert file uploaded text
        self.assert_text("uploaded successfully", "#wfu_messageblock_header_1_label_1")

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 –

I hope this post helped you out, let me know if you have any questions in the comments below!

Happy testing! 

My Resources

Thrive Suite: The all-in-one WordPress theme I used to create my blog.
Jasper AI: My favorite AI writing tool.
Surfer SEO:  A tool to help you rank your content on the first page of Google.

Write Content Faster


Write blog posts that rank for affiliate terms!

Join our mailing list

Unlock the Secrets to Becoming a SDET Pro in the Industry!

Stay ahead of the curve! Join our mailing list for exclusive insights, tips, and the latest industry updates delivered straight to your inbox

Table of Content

Related Post

equivalence and boundary value

Understanding Equivalence Partitioning and Boundary Value Analysis in Software Testing

Software testing techniques help ensure software quality and reliability, by helping us select test cases intelligently from the collection of test cases, such that all test scenarios are covered.

Boundary Value Analysis and Equivalence Partitioning are two testing techniques that help testers select a subset of test cases that covers all important test scenarios, even under time and budget constraints. By testing all the equivalence classes and boundary values, testers can reduce the risk of releasing software with defects.

Read More »