How to Handle Windows File Upload Using Selenium Webdriver

How to handle windows file upload using Selenium WebDriver?

// 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.

How to handle a window file upload pop up in selenium c#

This has been answered a few times on here but here is what you need to do. Find the input for the file upload and use send keys to the file path. You do not interact with the ui file upload dialog box.

ex:

        string filePath = "c:\Files\file.png";
driver.FindElement(By.XPath("//input[@type='file']")).SendKeys(filePath);

How to handle Windows file upload in .Net core using Selenium?

Zehra - It really depends on how precise you want to get to mimicking the user functionality. Typically you click a button that opens a windows dialog where you locate the file on your pc/phone. Since this is really not testing the application and is just a Windows function, I just use send keys. If you want to get more precise, you can look at AutoIT but I would suggest just doing send keys.

Set your location for the file.

 string filePath = @"C:\MyFiles\Test.jpg";

Then find the path to the input for the file upload.

 driver.FindElement(By.XPath("//div[@class='FileUploadInput']")).SendKeys(filePath);

If you have a spinner or a bar for the upload process, I would wait until that element is no longer visible and then proceed.

As an example - go here - https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_fileupload_get

In the example if you look at the "choose file" element, it looks like:

<input type="file" id="myFile">

You would then just do:

string filePath = @"C:\MyFiles\Test.jpg";
driver.FindElement(By.Id("myFile")).SendKeys(filePath);

How to handle windows file upload window when using selenium

You can do a nonblocking click by using either one of these:

The Advanced User Interactions API (JavaDocs)

WebElement element = driver.findElement(By.whatever("anything"));
new Actions(driver).click(element).perform();

or JavaScript:

JavascriptExecutor js = (JavascriptExecutor)driver;

WebElement element = driver.findElement(By.whatever("anything"));
js.executeScript("arguments[0].click()", element);


Related Topics



Leave a reply



Submit