Upload File
Problems in Selenium while handling window prompt
Selenium find some difficulties to find object and perform actions which is related to mouse or keyboard type of activity.It is difficult to handle Keyboard and mouse events/Action using selenium commands.There are some specific scenario where selenium command is launched but actual event did not fired.At that time Excecution stops and reports error.
Selenium doesn't provide support to handle browser pop-ups and window based pop ups.There are some methods in Robot class which are used to do the interaction with popups in WebApplications.For handling the pop-up and window-prompt we need to use Robot class..
What is Robot Class?
By using Robot Class we can perform actions on keyboard or mouse events.Robot class is very easy to use with automation process. It can be easily integrate with current automation framework.
Some of the methods of robot class which we are going to use
.keyPress();
KeyPress is a method if we pressctrl button of keyboard.
.keyRelease();
If we have press a ctrl button we have to release that button also.
Here i am giving you an example if i press ctrl v from keyboard.Then first i have to
Make an object of Robot Class and implement the robot methods.
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL); //This is for pressing ctrl button of Keyboard
r.keyPress(KeyEvent.VK_V); //This is for pressing v button of keyboard
Now we have to release that keys so we will write..
r.keyRelease(KeyEvent.VK_V); //This is for relasing the ctrl button of KeyBoard
r.keyRelease(KeyEvent.VK_CONTROL);//This is for releasing V button of keyBoard.
Here i this scenario i have used Robot class.
Scenario
1.Go to
http://api.checklist.com/login
2.Enter Email as rgulati883@gmail.com
3.Enter Password as jaiguruji
4.Click on Login
5.Click on Checklist1
6.Click on upload
7.Then add path of your File
8.Click on open button.
4Click on Checklist1(0)
5.Click on upload
6.So next step is we copy the path where our file on desktop.
// StringSelection ss = new StringSelection("Your file path ");
StringSelection ss = new StringSelection("C:\\Users\\Hp\\Desktop\\FileuploadTest.txt");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
When you do a cut or copy of text in the operating system, the text is
stored in the clipboard
The following method returns the content that is currently in the
clipboard.
// Calling key press event to press Enter Key from keyboard to place cursor in window textbox
r.keyPress(KeyEvent.VK_ENTER);
//Releaseing the Enter Key
r.keyRelease(KeyEvent.VK_ENTER);
8.After that we copy and paste that path in this FilepathText box.
/*
* Now we are going to trigger CTRL+V action so first we will press CTRL and then V and finally will
* release these key.
*/
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_CONTROL);
9.Now click on Open
// After pasting path now we are going to click on Open and that can be
//triggered by pressing enter key from Keyboard.
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
//Release open
10 File is added
package test;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestFileupload {
public static void main(String[] args) throws InterruptedException, AWTException {
System.setProperty("webdriver.chrome.driver","C:\\Users\\Hp\\Desktop\\Chromepth\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// WebDriver driver = new FirefoxDriver();
driver.get("http://api.checklist.com/login");
driver.findElement(By.xpath("//*[@id='email']")).sendKeys("rgulati883@gmail.com");
driver.findElement(By.xpath("//*[@id='loginForm']/div[2]/input")).sendKeys("jaiguruji");
driver.findElement(By.xpath("//button[@class='btn btn btn-primary pull-right']")).click();
Thread.sleep(8000);
driver.findElement(By.xpath("//*[@id='userChecklists']/li[1]/a/span")).click();
WebElement UploadImg = driver.findElement(By.xpath("//*[@id='taskUploadFile']"));
UploadImg.click();
// StringSelection ss = new StringSelection("Your file path ");
StringSelection ss = new StringSelection("C:\\Users\\Hp\\Desktop\\FileuploadTest.txt");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
Robot r = new Robot();
// Calling key press event to press Enter Key from keyboard to place cursor in window textbox
r.keyPress(KeyEvent.VK_ENTER);
//Releaseing the Enter Key
r.keyRelease(KeyEvent.VK_ENTER);
/*
* Now we are going to trigger CTRL+V action so first we will press CTRL and then V and finally will
* release these key.
*/
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_CONTROL);
// After pasting path now we are going to click on Open and that can be
//triggered by pressing enter key from Keyboard.
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
//Release open
}
}
//After using this code file is added.











Your code is not working.
ReplyDeleteAfter clicking the browse button window gets open but nothing happens after that
I have tried your code to try to upload a file and I am getting stuck at the point where the Upload window opens and path to the file gets pasted in the text box and then nothing happens. It is as if the enter key is not getting pressed and released after the pasting of file path. Following is my code:
ReplyDeleteStringSelection ss = new StringSelection("C:\\Users\\IBM_ADMIN\\Desktop\\Apps_For_Thick_Web_Consoles\\exe\\kbsetup.exe");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_V);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_V);
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
Any pointers you can give in how to troubleshoot this would be greatly appreciated.
hi hitesh, just give after the last line in the above code like
Deleter.delay(1000);
hi hitesh,sorry i misguided and not in the last line,it will come after the releasing control +v,i.e,r.keyRelease(KeyEvent.VK_V); after this u should use r.delay(1000); it worked for me
ReplyDeleteHow to close the opened frame??
ReplyDeleteWhat if i have to attach any file in gmail,& not the pasted one in particular..??
ReplyDeleteis that possible in ubuntu..??
Does this method work in IE..it works in other browsers...but seems not in IE...lpls confirm
ReplyDeleteThat's great! I always thought that once you brought up the Windows file upload dialog that Selenium stopped until you took the dialog down because Windows, not the browser, was now in control. My experiments even seemed to show that. Just to learn about the Robot functionality,I adapted your code, above, to mine and it worked!!! I was afraid I was going to have to take some drastic steps to get my file upload tests to work before I found this. Thank you! Thank you! Thank you!
ReplyDeleteDoes this work on a machine which uses Windows Server 2012 ? I tried this but it never pastes the filepath in filename input.
ReplyDelete@Musaffir: Even I am facing the same issue my file to upload script works in chrome but not in IE (I tried using both Robot & autoIT)). My IE browser version is 11.0.9600.18059 not sure is this only on this version of IE. Let me know if you find any alternative solution.
Delete@Musaffir: Even I am facing the same issue my file to upload script works in chrome but not in IE (I tried using both Robot & autoIT)). My IE browser version is 11.0.9600.18059 not sure is this only on this version of IE. Let me know if you find any alternative solution.
ReplyDeleteThank you... the code works after the correction is made
ReplyDeleteyou need more publicize this so many people who know about it are rare for people to know this
ReplyDeleteSalesforce Training in Hyderabad
nice post about uploading in selenium..
ReplyDeleteSAS Institute introduced the SAS Certified Professional Program,training proper understanding of how the SAS software works. Among the five certification programs that SAS Institute has come up with, SAS training can be considered as the entry point into the big data and the data analytics industry.
SAS online training in hyderabad
This works fine for me but when switch to web page I received (org.openqa.selenium.UnhandledAlertException: Modal dialog present:
ReplyDeleteException and not allow me to proceed
How can we upload multiple files using robot class?kindly help me to proceed
ReplyDeleteThe Blog gave me idea to upload file in selenium My sincere thanks for sharing this post
ReplyDeleteSelenium Training in Bangalore
Selenium Training in BTM Layout
Selenium Training in Marathahalli
nice blog has been shared by you before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blogs.
ReplyDeleteSelenium Training in Bangalore
nice blog has been shared by you. before i read this blog i didn't have any knowledge about this. so keep on sharing such kind of an interesting blogs.
ReplyDeleteDevops Training in Bangalore
that nice idea for franchise.............
ReplyDeleteFranchise Opportunities | Franchise Opportunities India
Very good post, it's very useful to all blog readers and Keep sharing.
ReplyDeleteImage Processing Project Center in Chennai | Image Processing Project Center in Velachery
Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts, have a nice weekend!
ReplyDeletejava Training in Bangalore
Thank you for offering such a unique information really helpful for learners one of the recommended blog... Selenium Training in Chennai | Selenium Training institutes in Chennai
ReplyDelete