0

On an Ubuntu machine I try to automate file upload but I am unable to do so in Chrome using selenium. I tried with Robot class and normal send keys method.

Please refer my code:

StringSelection select = new StringSelection("/home/manojnn/Desktop/OrderDetails.xlsx"); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(select,null);

     System.out.println("selection" +select);
     driver.findElement(By.xpath("//label[text()='Upload']")).click();
    Thread.sleep(3000);
    Robot robot = new Robot();
     Thread.sleep(1000);

      // Press Enter
     robot.keyPress(KeyEvent.VK_ENTER);

    // Release Enter
     robot.keyRelease(KeyEvent.VK_ENTER);

      // Press CTRL+V
     robot.keyPress(KeyEvent.VK_CONTROL);
     robot.keyPress(KeyEvent.VK_V);

    // Release CTRL+V
     robot.keyRelease(KeyEvent.VK_CONTROL);
     robot.keyRelease(KeyEvent.VK_V);
     Thread.sleep(1000);


     robot.keyPress(KeyEvent.VK_ENTER);
     robot.keyRelease(KeyEvent.VK_ENTER);
3
  • 1
    Show us how you've tried and provide HTML for target input Commented Aug 16, 2016 at 6:42
  • I have updated the information..please help me asap Commented Aug 16, 2016 at 6:51
  • Possible duplicate of How to Upload files using Selenium webdriver in Java Commented Aug 16, 2016 at 7:48

3 Answers 3

3

You don't need to click on upload button and handle upload prompt, but just to send path to file to appropriate input field. Try following solution and let me know if it not works:

driver.findElement(By.xpath('//input[@type="file"]')).sendKeys("/home/manojnn/Desktop/OrderDetails.xlsx"); 
Sign up to request clarification or add additional context in comments.

1 Comment

You don't need to handle OS native pop ups!
1

You can use the external tool like AUTOIT and can use it with selenium for upload purpose. Try this link https://www.seleniumeasy.com/selenium-tutorials/upload-a-file-using-selenium-webdriver-with-autoit

Comments

-1

for file upload you need to do directly pass file path on the uploadfile text field. use beloe driver.findElement(By.xpath(uploadTextFiledElementId)).sendKeys("filepath");

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.