0

Just reviewed questions that may already have the answer, but failed to find anything relevant.

So the site I am testing has a file upload dialog. When you click Select File -> Browse, a Windows File Upload dialog appears when you have to type (or choose with mouse clicks) the path to the file. When you are done, you have to click the Open button. So WebDriver doesn't see these Windows File Upload controls, so I am using Sikuli and AutoIt to type a path and click Open. Both tools work, but this part is unstable. You have to care that windows are not minimized, for example. Another challenge is making sure that wrong characters didn't come from clipboard right before you type the text in the path input field.

One more option I considered was to use something like Post request to the server, but I can't emulate it, since the site uses tokens to cipher all the traffic.

So the question: would you recommend any other options/tools except AutoIt and Sikuli to handle this Windows File Upload dialog?

Thanks, Racoon

  • 1
    It possible to upload a file with Selenium alone. Simply call driver.sendKeys("full file path") on the <input> element. Depending on the implementation of the page you may have to cancel the click event on on the <input> with a script injection to avoid the Windows dialogue. – Florent B. Feb 6 '18 at 16:21
  • @Florent, thanks, very interesting. This element has readonly attribute. Also, judging by page source code, a js script is used to handle all these operationgs including opening Windows dialog. As a result, when I go "normal" way with no inject, type a path and click Open, a file content is being downloaded even before I click Send File button. If I am to use a script injection, I would only type a path to file in this field, but no actual upload will happen. Anyway, reading about a script injection would be useful. Do you have any links with examples to read? Thanks! – Racoon Feb 6 '18 at 17:04
  • 1
    first inject driver.executeScript("document.addEventListener('click',function handler(event){if(event.target.type==='file')event.preventDefault()},true)) in the page to prevent the dialogue, then execute all the clicks just like a real user and finally call driver.findElement(By.css("input[type=file]")).sendKeys("full file path") to assign the file. – Florent B. Feb 6 '18 at 17:15
  • 1
    See this canonical question: sqa.stackexchange.com/questions/12851/… – LittlePanda Apr 11 '18 at 12:14
1

I also faced the same problems using AutoIT and Sikuli both are not give stable solutions for me for my works.

Now i'm using Robot class with selenium for upload file

Click File Upload Button

driver.findElement(By.xpath("//*[@id='photofile']")).click();

Copy file path of uploading file

StringSelection ss = new StringSelection("E:\\Capture.PNG");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

Paste that path using Robot Class and press Enter

Robot robot = new Robot();
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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