5

I'd like to side step the dialog box and upload a file using the sendKeys method but it doesn't seem to work. Is anything wrong with the following code?

WebDriver driver = new FirefoxDriver();
String filename = "test.txt";
File file = new File(filename);       
String path = file.getAbsolutePath();

driver.get("http://www.megafileupload.com/");
driver.manage().window().maximize();
driver.findElement(By.cssSelector("i.fa.fa-upload")).click();
driver.findElement(By.cssSelector("div[class=internal] i.entypo-upload")).sendKeys(path);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.cssSelector("button.btn.btn-green.btn-lg")).click(); //upload button
8
  • hi selenium webdriver wont support windows dialogues handling, you have two options to do that 1) auto it 2) make browser to auto download. Commented Feb 15, 2016 at 8:42
  • Can you check the answers here : sqa.stackexchange.com/questions/12851/… Commented Feb 15, 2016 at 8:45
  • @sameerjoshi That is why I mentioned "side step" meaning to avoid the dialog box and inject file using sendKeys method which is valid. As for your 2nd point - This is about "upload" not download
    – ilm
    Commented Feb 15, 2016 at 8:57
  • Can you tell the error? Because when we click on "select file" button, then it open a pop-up window, that maybe the reason of not find an element.
    – siraj
    Commented Feb 15, 2016 at 9:54
  • error- "Element is not currently visible and so may not be interacted with" That's because the upload button, is not visible since the file upload didn't take place. With Selenium sendKeys method of uploading the file you do not click on "select-file". You send the full path of the file into the form.
    – ilm
    Commented Feb 15, 2016 at 12:32

2 Answers 2

7

If you want to use sendKeys() to upload a file using selenium web driver, make sure that the input element is visible on the page. Once the element is visible, you should simply use below mentioned code:

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

You should not click on the browse button as it will open windows dialogue box and selenium web driver won't work.

myUploadElement is the id of that element 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.

If sendkeys() wont work at all, then you can also use SikuliX to handle uploading files.

SikuliX can be used as it automates everything that you see on the screen. It uses image recognition to identify and control GUI components.

1
  • Yeah I figured out it was the input tag that was required for it to work.
    – ilm
    Commented Feb 17, 2016 at 8:55
1

sendKeys(); function works only in some scenarios, you have following other options to deal with upload scenarios.

  1. Use AutoIT tool for upload, but this works only for windows machine

  2. Use ROBOT class of java for upload function, ROBOT class simulates the actual keyboard functions.

Following is the code using ROBOT class:

public class UploadFile {

Property prop = new Property();

/*
 *  Funtion to set the value in clipboard
 */
private static void setClipboardData(String string) {
    // StringSelection is a class that can be used for copy and paste
    // operations.
    StringSelection stringSelection = new StringSelection(string);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}

/*
 *  Function to upload the file using robot class
 */
public void upload(String fileLocation) throws Exception {

    // Setting clipboard with file location
    setClipboardData(fileLocation);

    // native key strokes for CTRL, V and ENTER keys
    Robot robot = new Robot();

    Thread.sleep(2000);

    switch (prop.get("GlobalConfig.os").toLowerCase()) { // Pass your OS platform name here, I am using properties file where OS name is saved you can as a string.

    case "mac":

        if (!prop.get("GlobalConfig.browser").equalsIgnoreCase("chrome")) { // In mac machine for chrome you need to switch focus to upload dialog again I have saved browser name in properties file you can pass it as string.
            robot.keyPress(KeyEvent.VK_META);
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_META);
            robot.keyRelease(KeyEvent.VK_TAB);
        }
        robot.delay(2000);
        robot.keyPress(KeyEvent.VK_META);
        robot.keyPress(KeyEvent.VK_SHIFT);
        robot.keyPress(KeyEvent.VK_G);
        robot.keyRelease(KeyEvent.VK_META);
        robot.keyRelease(KeyEvent.VK_SHIFT);
        robot.keyRelease(KeyEvent.VK_G);
        robot.keyPress(KeyEvent.VK_META);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_META);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        robot.delay(2000);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        break;

    case "windows":

        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);
        break;

    }

}
}
1
  • Under which circumstances does sendKeys() work? Is there a way to modify the HTML to allow for it? Commented Oct 5, 2017 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.