How to upload a file using Selenium WebDriver in Java

Can someone let me know how to upload a file using Selenium using Java code?

When I click the button in the application, it opens in a new window, which I can use to select the download file. Browse button developed by Silverlight (C #).

+46
java upload selenium-webdriver
Jun 03 '13 at 12:26
source share
6 answers

First make sure the input element is visible

As Mark Collin stated in the discussion here :

Do not click on the browse button, this will cause an OS-level dialog and effectively stop your test.

Instead, you can use:

driver.findElement(By.id("myUploadElement")).sendKeys("<absolutePathToMyFile>");

myUploadElement is the identifier of this element (button in this case), and in sendKeys you must specify the absolute path of the content you want to download (image, video, etc.). Selenium will do the rest for you.

Keep in mind that loading will only work if the item you submit must be in the form <input type="file">

+71
Jun 04 '13 at 2:35 am
source share
 driver.findElement(By.id("urid")).sendKeys("drive:\\path\\filename.extension"); 
+9
Aug 07 '13 at 12:04 on
source share

This is what I use to upload the image through the download 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); 

to do

+8
Oct 16 '14 at 5:47
source share

If you have a text box to enter the path to the file, just use sendkeys to enter the path to the file and click the submit button. If there is no text box to enter the path to the file and just click the browse button and select the file from the popup window, you can use the AutoIt tool, see the next step to use AutoIt for this,

  • Download and install the Autoit tool from http://www.autoitscript.com/site/autoit/

  • Open Programs → Autodiscover Tool → SciTE Script Editor.

  • Paste the following code into the Autoit editor and save it as "filename.exe" (for example: new.exe)

    Then compile and create the file to make it exe. (Tools → Compile)

Startup 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 compile from the Tools menu of the Autoit tool → SciTE Script.

Paste the Java code below into the 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. 
+2
Aug 03 '14 at 6:02
source share

I tried to use the above robot, you need to add a delay :( also you cannot debug or do something else because you lose focus :(

// open the download 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); 
+2
Nov 09 '15 at 14:17
source share

Find the tag as type="file" . This is the main tag supported by selenium. If you can build XPath with the same when recommended.

  • use the submit buttons for the viewable button (the button that opens your window for selecting files)
  • Now click the button that will load your file

As shown 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(); 

To download multiple files, put all the files one at a time using sendkeys, and then click the Download button

 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 
-one
Jul 08 '15 at 14:17
source share



All Articles