67

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 select upload file. The browse button developed by Silverlight (C#).

1
  • Welcome Mahesh - can you give us a bit more background please? Perhaps post a screenshot and/or the code of your web application, and show us the webdriver code you've developed so far?CommentedJun 3, 2013 at 13:18

6 Answers 6

101

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">

3
  • 1
    Hi @ArupRakshit, it would be great if you give the gist of code here. So that we can take a look on why it is not working. Thanks.CommentedOct 1, 2013 at 17:08
  • 6
    You can use following code to get absolute path of the file and upload the content String filePath = System.getProperty("user.dir") + "/src/res/test.pdf; driver.findElement(By.id("elementID")).sendKeys(filePath);CommentedJun 1, 2015 at 8:15
  • 1
    Yes, in this case the attribute <input type="file"> is very important focusing.CommentedFeb 3, 2016 at 8:03
12
driver.findElement(By.id("urid")).sendKeys("drive:\\path\\filename.extension"); 
0
    12

    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

    2
    • Sweet. But this are key events not mouse events (obviously). Anyhow great idea... .CommentedDec 11, 2014 at 19:59
    • it does not close the upload window for me
      – mosaad
      CommentedJul 14, 2016 at 14:31
    4

    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,

    1. Download and Install Autoit tool from http://www.autoitscript.com/site/autoit/

    2. Open Programs -> Autoit tool -> SciTE Script Editor.

    3. 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. 
    1
    3

    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); 
    2
    • Can you explain the robot keypress steps what exactly happing when above commands are called?CommentedJun 17, 2016 at 10:49
    • It's pressing enter, pasting text, and pressing enter again
      – blizz
      CommentedApr 15, 2017 at 15:26
    0

    I tried many things, but the only thing that worked for me is creating a file on the fly and using that...here's the solution in C# .NET

    string filePathName = Path.Combine(Directory.GetCurrentDirectory(), "myFile.txt"); File.WriteAllText(filePathName, "File contents."); IWebElement fileSelector = driver.FindElement(By.id("myUploadElement")); fileSelector.SendKeys(filePathName); 

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.