1

Possible Duplicate:
How to upload a file from a site using Selenium's java inteface

I am new to Selenium.Can you please tel me how to automate file upload in Internet Explorer using Selenium?

Community
  • 1
  • 1
Nithi
  • 176
  • 1
  • 3
  • 17

2 Answers2

4

It is not easy, and it is not easy for very good reasons - security. If you are able to upload something like this, what's stopping someone uploading your details using the same method?

You have also given us no example to work with so:

Given this sample webpage:

<html>
<head>
<style type="text/css">
.fileSave { color: red; }
</style>
</head>
<label for="fileUpload">File location:
<input type="file" id="fileUpload" />
<br>
<br>
<a href="" class="fileSave">Save file</a>
</body>
</html>

I can do this, in C#:

Driver = new ChromeDriver();
var fileUploadControl = Driver.FindElement(By.Id("fileUpload"));
fileUploadControl.SendKeys("C:\File.txt");
var submitLink = Driver.FindElement(By.ClassName("fileSave"));
submitLink.Click();
Arran
  • 24,648
  • 6
  • 68
  • 78
  • "If you are able to upload something like this, what's stopping someone uploading your details using the same method?" To some extent, CAPTCHA will. – fixxxer Sep 12 '12 at 02:23
3

This has been asked several times and is also in some Selenium FAQ.

Selenium 2 (WebDriver) Java example:

// assuming driver is a healthy WebDriver instance
WebElement fileInput = driver.findElement(By.xpath("//input[@type='file']"));
fileInput.sendKeys("C:/path/to/file.jpg");

For Selenium RC, see this question.

The idea is to directly send the absolute path to the file to an element which you would usually click at to get the modal window - that is <input type='file' /> element.

Community
  • 1
  • 1
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145