Can anyone let me know how to upload a file using Selenium by Java code?
When I click on button in the application it gets open in the new window what I can use to s
This is what I use to upload the image through upload window:
//open upload window
upload.click();
//put path to your image in a clipboard
StringSelection ss = new StringSelection("C:\\IMG_3827.JPG");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
//imitate mouse events like ENTER, CTRL+C, CTRL+V
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
done
First make sure that the input element is visible
As stated by Mark Collin in the discussion here:
Don't click on the browse button, it will trigger an OS level dialogue box and effectively stop your test dead.
Instead you can use:
driver.findElement(By.id("myUploadElement")).sendKeys("<absolutePathToMyFile>");
myUploadElement is the id of that element (button in this case) and in sendKeys you have to specify the absolute path of the content you want to upload (Image,video etc). Selenium will do the rest for you.
Keep in mind that the upload will work only If the element you send a file should be in the form <input type="file">
I have tried to use the above robot there is a need to add a delay :( also you cannot debug or do something else because you lose the focus :(
//open upload window upload.click();
//put path to your image in a clipboard
StringSelection ss = new StringSelection(file.getAbsoluteFile());
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
//imitate mouse events like ENTER, CTRL+C, CTRL+V
Robot robot = new Robot();
robot.delay(250);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.delay(50);
robot.keyRelease(KeyEvent.VK_ENTER);
Find the tag as type="file"
. this the main tag which is supported by selenium. If you are able to build your XPath with same when it is recommended.
As below :-
driver.findElement(By.xpath("//input[@id='files']")).sendKeys("D:"+File.separator+"images"+File.separator+"Lighthouse.jpg"");
Thread.sleep(5000);
driver.findElement(By.xpath("//button[@id='Upload']")).click();
For multiple file upload put all files one by one by sendkeys and then click on upload
driver.findElement(By.xpath("//input[@id='files']")).sendKeys("D:"+File.separator+"images"+File.separator+"Lighthouse.jpg"");
driver.findElement(By.xpath("//input[@id='files']")).sendKeys("D:"+File.separator+"images"+File.separator+"home.jpg");
driver.findElement(By.xpath("//input[@id='files']")).sendKeys("D:"+File.separator+"images"+File.separator+"tsquare.jpg");
Thread.sleep(5000);
driver.findElement(By.xpath("//button[@id='Upload']")).click(); // Upload button
If you have a text box to type the file path, just use sendkeys to input the file path and click on submit button. If there is no text box to type the file path and only able to click on browse button and to select the file from windows popup, you can use AutoIt tool, see the step below to use AutoIt for the same,
Download and Install Autoit tool from http://www.autoitscript.com/site/autoit/
Open Programs -> Autoit tool -> SciTE Script Editor.
Paste the following code in Autoit editor and save it as “filename.exe “(eg: new.exe)
Then compile and build the file to make it exe. (Tools → Compile)
Autoit Code:
WinWaitActive("File Upload"); Name of the file upload window (Windows Popup Name: File Upload)
Send("logo.jpg"); File name
Send("{ENTER}")
Then Compile and Build from Tools menu of the Autoit tool -> SciTE Script Editor.
Paste the below Java code in Eclipse editor and save
Java Code:
driver.findElement(By.id("uploadbutton")).click; // open the Upload window using selenium
Thread.sleep("20000"); // wait for page load
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "C:\\Documents and Settings\\new.exe"); // Give path where the exe is saved.
driver.findElement(By.id("urid")).sendKeys("drive:\\path\\filename.extension");