How to Use Auto It to Upload Files Using Selenium
This guide volition show you lot how to Upload File in Selenium Webdriver using Autoit Step by Step guide in a simple manner.
1-While automating web-application many times you will get window-based activity similar- file upload, file download pop-up, window authentication for secure sites, etc. In this case, Selenium will fail and volition non be able to handle desktop elements.
We can upload files in Selenium using different ways
one- Using sendKeys method
two- Using Robot course
three- Using AutoIT
4- Using WGET
Permit me explicate to you the easiest way which works most of the time using sendKeys
Syntax
driver.findElement(By.xpath("xpath of whatsoever chemical element")).sendKeys("File Path");
Example
driver.findElement(By.xpath("//input[@type='file']")).sendKeys("C:\\Downloads\\Mukesh.png");
or
driver.findElement(Past.xpath("//input[@type='file']")).sendKeys("C:/Downloads/Mukesh.png");
Explanation- You do not have to click on file uploader because if you lot click on the file upload push button, it will showtime file uploader which you demand to handle separately.
You lot can find the locator of the chemical element where the file needs to be updated and then you tin pass the file path direct, files can exist excel, png or whatsoever type depends on your application.
Annotation- In case of sendKeys method does not piece of work then you can go ahead with all other options.
Introduction to AutoIT tool
1-AutoIt is a freeware automation tool that can work with desktop applications too.
2-It uses a combination of keystrokes, mouse move and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.1000. VBScript and SendKeys).
For more info about AutoIT, yous can visit their Official WebsiteAutoIt Official Website
YouTube video for Same
How to write a script in AutoIT?
For AutoIt scripting, y'all should accept iii things ready.
1-AutoIt Editor- Editor helps us to write AutoIt scripts.
ii-Tool Finder – It will help u.s. to place the chemical element and check their Attributes.
3- AutoIt Help section- This help you to understand about AutoIt functions and what are the parameter it accepts.
Let's showtime with Downloading offset
Pace 1– Navigate to AutoIt official website https://world wide web.autoitscript.com/site/autoit/downloads/ and go to download department or Click here Download AutoIt
Stride two– Click on Download AutoIt and Install
Pace 3– Click on Download Editor and Install.
Step iv–Once both are installed in your machine, check all are installed correctly.
Note- Generally it goes to C:\Program Files\AutoIt3 location if yous do not change it
Step5– Open SCiTE folder and Click on SciTE this will open up AutoIt Editor
Once all Installed Allow'south see how we can write the script.
Upload File in Selenium Webdriver using Autoit
To Upload File in Selenium Webdriver using AutoIt we need to take care of some steps so let'southward begin
To upload a file in Selenium Webdriver we volition create AutoIT script, which volition handle file-uploaded window, and and so we will combine Selenium script with AutoIt scripts.
Click on the Upload button you will go a file uploader we volition handle the aforementioned using AutoIt.
Step 1- Open Editor and Finder Tool
Stride 2– We demand to write script to upload file then we volition use some method of AutoIt.
Each method will have some own functionality
ControlFocus-This will requite focus on the window
ControlSetText-This will gear up the file path
ControlClick-This will click on button
Pace i-
Click on Browse button , a new window will open now open finder tool and Click on Finder tool and drag to the file name as I shown in below screenshot.
This will requite all the detail about that window and file name department info; we volition use simply some attributes like window title, class, and instance.
Open up AutoIt Editor and Write Script
In ControlClick method, nosotros will give command id of open button
Pace two-
Salve the script to a particular location with some unique name.
Note- Past default script volition be saved as .au3 extension
Step 3– Now Compile the script then for compiling right click on file and Select compile script this volition generate a .exe file of the file.
Step four- Now write Selenium plan and add this .exe file and run your program
Here is the code
1 2 3 four 5 6 vii eight 9 10 xi 12 13 14 15 xvi 17 18 19 xx 21 22 23 24 25 26 27 28 29 30 31 32 33 | bundle demo ; import org . openqa . selenium . By ; import org . openqa . selenium . WebDriver ; import org . openqa . selenium . firefox . FirefoxDriver ; public class DemoFileUpload { public static void main ( String [ ] args ) throws Exception { // This will open Firefox browser WebDriver driver = new FirefoxDriver ( ) ; // This will maximize browser to full screen commuter . manage ( ) . window ( ) . maximize ( ) ; // This will open respective URL driver . get ( "your application url" ) ; // This volition click on Upload button commuter . findElement ( By . xpath ( "//*[@blazon='file']" ) ) . click ( ) ; // This will invoke AutoIT script hither give the path of the script //and this will throw IO exception and so u can utilise throw or try catch // In my case I am using throws Runtime . getRuntime ( ) . exec ( "C:\\Users\\mukesh_otwani\\Desktop\\AutoItScripts\\blogUpload.exe" ) ; // Once you will run this program AutoIt script volition be invoked and respective f//ile will exist attached } } |
Thanks for visiting my blog. Please get out a annotate below if you are finding any result while Upload File in Selenium Webdriver using Autoit.
Proceed in touch. Have a prissy day.
Now if you are comfortable with Basic Selenium, you lot can bank check out Advance Selenium Tutorial as well. 🙂
For More updatesLearn Automation page
For whatsoever query bring together Selenium grouping-Selenium Group
Source: https://learn-automation.com/upload-file-in-selenium-webdriver-using-autoit/
0 Response to "How to Use Auto It to Upload Files Using Selenium"
Post a Comment