File Upload

In previous section, we have understood about the Authentication popup handling in Selenium Python. Now in this section we will discuss more how to handle the File upload scenario  in Selenium Python.

Objective:

  • Introduction
  • Scenario to process for File Upload
  • Practical Example

Introduction:

File uploading is a common scenario in almost all website. This is very important concept which every professional should be aware of.

To learn this concept we will use http://demo.automationtesting.in/FileUpload.htmlurl.

In the above image you can see there is a browser button with Select file name. Once we click on the Browser file button it will ask to choose a file. To achieve this we can to follow below steps.

  1. Click on the button
  2. Choose the file
  3. Click on Open

Can we automate 2nd step in Selenium. No, You cannot handle the 2nd step directly in selenium as this is your machine. For this you can use AutoID / SQLi to automate this directly.

There is another way for Selenium is we can pass the url of the file with send_keys() method. Let’s have a look on below practical example how we can upload file in Selenium Python using the send_keys().

Example:

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chromeimport ChromeDriverManager
import time

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('http://demo.automationtesting.in/FileUpload.html')
driver.maximize_window()

file_uplaod = driver.find_element(By.NAME, 'input4[]')
file_uplaod.send_keys('/F:/Capture.png')

Output:

From the above example you can see that after successful execution of code it will upload the image file successfully and displayed in front end.

Summary:

  • File uploading is a common scenario in almost all website. This is very important concept which every professional should be aware of.
  • Once we click on the Browser file button it will ask to choose a file. To achieve this we can to follow below steps.
    • Click on the button
    • Choose the file
    • Click on Open
  • There is another way for Selenium is we can pass the url of the file with send_keys() method. Let’s have a look on below practical example how we can upload file in Selenium Python using the send_keys().

Leave a Reply

Your email address will not be published. Required fields are marked *