Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle windows file upload using Selenium WebDriver?

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?

Screenshot

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/ 
like image 824
Jasmine.Olivra Avatar asked Jun 29 '12 05:06

Jasmine.Olivra


People also ask

Can we upload file using Selenium WebDriver?

We can upload files using Selenium Webdriver. This is achieved by the sendKeys method. We have to first identify the element which performs the file selection by mentioning the file path [to be uploaded].

Which tool is used to upload a file in Selenium?

We have following options to upload files in a Remote Selenium WebDriver: SendKeys. Robot Class. AutoIT tool.


2 Answers

// assuming driver is a healthy WebDriver instance WebElement fileInput = driver.findElement(By.name("uploadfile")); fileInput.sendKeys("C:/path/to/file.jpg"); 

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:

driver.findElement(By.id("inputFile")).sendKeys("C:/path/to/file.jpg"); 

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:

driver.findElement(By.id("SWFUpload_0")).click(); Robot r = new Robot(); r.keyPress(KeyEvent.VK_C);        // C r.keyRelease(KeyEvent.VK_C); r.keyPress(KeyEvent.VK_COLON);    // : (colon) r.keyRelease(KeyEvent.VK_COLON); r.keyPress(KeyEvent.VK_SLASH);    // / (slash) r.keyRelease(KeyEvent.VK_SLASH); // etc. for the whole file path  r.keyPress(KeyEvent.VK_ENTER);    // confirm by pressing Enter in the end r.keyRelease(KeyEvent.VK_ENTER); 

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:

First, you modify the source code of you the flash application, exposing internal methods using the ActionScript's ExternalInterface API. Once exposed, these methods will be callable by JavaScript in the browser.

Second, now that JavaScript can call internal methods in your flash app, you use WebDriver to make a JavaScript call in the web page, which will then call into your flash app.

This technique is explained further in the docs of the flash-selenium project. (http://code.google.com/p/flash-selenium/), but the idea behind the technique applies just as well to WebDriver.

like image 94
Petr Janeček Avatar answered Oct 08 '22 11:10

Petr Janeček


Below code works for me :

public void test() {     WebDriver driver = new FirefoxDriver();     driver.get("http://www.freepdfconvert.com/pdf-word");     driver.findElement(By.id("clientUpload")).click();     driver.switchTo()             .activeElement()             .sendKeys(                     "/home/likewise-open/GLOBAL/123/Documents/filename.txt");     driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);     driver.findElement(By.id("convertButton")); 
like image 39
Emmanuel Angelo.R Avatar answered Oct 08 '22 12:10

Emmanuel Angelo.R