javafile-uploadselenium-webdriver

File Upload using Java Robot API with Selenium WebDriver by Java


How can I upload files using Java Robot API with Selenium WebDriver by Java?

I am having problems uploading a file in a test Environment with Selenium WebDriver. I use the selenium WebDriver and Java.


Solution

  • Actually, there is an in-built technique for this, too. It should work in all browsers and operating systems.

    Selenium 2 (WebDriver) Java example:

    // assuming driver is a healthy WebDriver instance
    WebElement fileInput = driver.findElement(By.xpath("//input[@type='file']"));
    fileInput.sendKeys("C:/path/to/file.jpg");
    

    The idea is to directly send the absolute path to the file to an element which you would usually click at to get the modal window - that is <input type='file' /> element.