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 – https://the-internet.herokuapp.com/upload

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
        self.open("https://the-internet.herokuapp.com/upload")

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

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

        # click the upload button
        self.click("#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 – https://practice.automationbro.com/cart/

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
        self.open("https://practice.automationbro.com/cart/")

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

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

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

        # click the upload button
        self.click("#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

5/5

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

7 Principles of Software Testing

7 Principles of Software Testing with Examples

Software testing plays a crucial role in the software development life cycle as it helps ensure the quality, reliability, and performance of the final product. Software testing is not just about finding defects; it also provides valuable insights into the overall quality of the software. By conducting effective software testing, it shows how well the application behaves in different scenarios and whether it meets user expectations.

Read More »
Differences Between CI/CD and DevOps

Key Differences Between CI/CD and DevOps

Software development is far from a simple journey that involves merely executing code and launching applications. It often presents challenges, as it’s rare to get everything right from the start. Each time you implement new or frequent code changes, you inadvertently introduce potential issues that can be time-consuming to resolve. This complexity necessitates a structured approach to ensure smooth production.

Read More »