I have seen lots of questions and solutions on File upload using Selenium WebDriver on Stack Overflow. But none of them are working for following scenario.
Someone has given a solution as following
// assuming driver is a healthy WebDriver instance
WebElement fileInput = driver.findElement(By.name("uploadfile"));
fileInput.sendKeys("C:/path/to/file.jpg");
But still I can't find window handle. How can I work on that?
I am looking for a solution for the scenario above.
Please check this on any of the following websites.
http://www.uploadify.com/demos/
http://www.zamzar.com/
Best Answer
Hey, that's mine from somewhere :).
In case of the Zamzar web, it should work perfectly. You don't click the element. You just type the path into it. To be concrete, this should be absolutely ok:
In the case of the Uploadify web, you're in a pickle, since the upload thing is no
input
, but a Flash object. There's no API for WebDriver that would allow you to work with browser dialogs (or Flash objects).So after you click the Flash element, there'll be a window popping up that you'll have no control over. In the browsers and operating systems I know, you can pretty much assume that after the window has been opened, the cursor is in the
File name
input. Please, make sure this assumption is true in your case, too.If not, you could try to jump to it by pressing Alt + N, at least on Windows...
If yes, you can "blindly" type the path into it using the
Robot
class. In your case, that would be something in the way of:It sucks, but it should work. Note that you might need these: How can I make Robot type a `:`? and Convert String to KeyEvents (plus there is the new and shiny
KeyEvent#getExtendedKeyCodeForChar()
which does similar work, but is available only from JDK7).For Flash, the only alternative I know (from this discussion) is to use the dark technique: