File Upload using Selenium


File Upload: 
There are different ways to handle file uploads with Selenium Webdriver.
Python Selenium Webdriver comes with native file upload feature. Uploading files in WebDriver is done by simply using the sendKeys() method on the file-select input field to enter the path to the file to be uploaded. There is no setup required for running tests with file upload, all we need to do is use the sendKeys command to type the local path of the file in the file field.

HTML code should look similar to this :
The following is the scenario to upload a file using sendKeys() in selenium webdriver without using any third party tools:

Scenario 1:
Open Chrome browser.
Navigate to the https://html.com/input-type-file/ application.
Upload a file then click on submit button.

Script:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
import time
chromePath=
"C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("https://html.com/input-type-file/") 
frmobj=driver.find_element(By.XPATH,"//input[@id='fileupload']")
frmobj.send_keys(
"C:\\TempFiles\\text1.txt")
# we are using submit() method instead of click
driver.find_element_by_xpath(
"//input[@type='submit' and @value='submit']").submit()
txtmsg=driver.find_element_by_xpath(
"//h1[@class='title-page']").text

if(txtmsg=="Oops! That page can’t be found."):
   
print("File has been uploaded")
else:
   
print("File has not been uploaded")

NOTE: It will works only when the textbox is enabled. So please make sure that the input element is visible. if there is no text box to set the file path and only able to click on Browse button to upload the file in the windows popup box then we do upload a file using third party tools like autoIT.

Another way to upload a file

You will have only a button (browse button) and when click on browse button, browser opens a window popup and to select the file from windows, And as we know selenium will not support OS controls.
We can use AutoIt tool (open source tool) which takes the responsibility to upload the file.

AutoIt Introduction:
AutoIt Tool is an open source tool. It is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying “runtimes” required!
For AutoIt scripting, you should have three things ready.
1-AutoIt Editor- Editor helps us to write AutoIt scripts. 
2-Tool Finder (Same as firebug on Firefox) – It will help us to identify the element and check their Attributes. 
3- AutoIt Help section- This help you to understand about AutoIt functions and what are the parameter it accepts.

Step 1:Download Autoit tool from AutoIt website
Step 2– Click on Download AutoIt and Install
Step 3–  Click on Download Editor and Install.
Once both installed in your machine check all is installed correctly.
Note- Generally it goes to C:\Program Files\AutoIt3 or C:\Program Files (x86)\ AutoIt3 location if you do not change it
Note: No need to write any script in AutoIt editor we can write script in python editor after install AutoIt package in python editor. Here we are using only finder tool for identifying the object of file upload window
Step5– Navigate AutoIt folder and Click on Au3Info.exe this will open finder tool. 

Step 6- Click on Finder tool and drag to the file name as shown in below screenshot.
This will give all the detail about that window and file name section info; you will use some attribute like window title, class, and instance.
We need to write script to upload file so we will use some methods of AutoIt. Each method will have some own functionality.
Win_wait - it will wait till file upload window loads.
Control_set_text - it will set the file path.
Control_click - it will click on the buttons.
You want learn more on AutoIt please refer below url for documentation.
How to write a python script for upload a file , before that we have to install autoit and PyAutoIt packages into your IDE.
The following is the scenario to upload a file in selenium webdriver using any third party tools(AutoIt):

Scenario 2 :
Open Chrome browser.
Navigate to the https://html.com/input-type-file/ application.
Upload a file then click on submit button.

Script:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.color import Color
import autoit
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get(
"https://html.com/input-type-file/")
driver.find_element(By.XPATH,"//input[@id='fileupload']").click()
time.sleep(3)
handle =
"[CLASS:#32770; TITLE:Open]"
autoit.win_wait(handle, 60)
autoit.control_set_text(handle,
"Edit1", "C:\\TempFiles\\text1.txt")
time.sleep(
3)
autoit.control_click(handle,
"Button1")
time.sleep(
3)
# we are using submit() method instead of click
driver.find_element_by_xpath("//input[@type='submit' and @value='submit']").submit()
txtmsg=driver.find_element_by_xpath(
"//h1[@class='title-page']").text

if(txtmsg=="Oops! That page can’t be found."):
   
print("File has been uploaded")
else:
   
print("File has not been uploaded")


Comments