0

I am trying to upload a file using selenium web driver in c#.

I am trying this: WebDriver.FindElement(By.Name("upload")).SendKeys("C:\Users\test\Desktop\test.cs");

but this is not working.

This is working properly in selenium IDE but not in selenium webdriver.

Following is the HTML code of the page:

where I am trying to use following element: <input class="js-attach-file" type="file" tabindex="-1" name="upload">

<!DOCTYPE html>
<html lang="en">
<head>
<body class="page-index firefox firefox-9 windows extra-large-window full-content window-up">
<div id="nocss">
<div id="surface" class="clearfix" style="height: 725px;">
<div class="window-overlay">
<div class="window" style="left: 375px; top: 50px; display: block;">
<div class="window-wrapper clearfix">
<div>
<div class="window-header-utils">
<div class="window-header clearfix">
<div class="window-main-col">
<div class="window-sidebar">
<div class="window-module card-label-list clearfix hide">
<div class="window-module clearfix">
<div class="window-module clearfix attachments">
<h3>Attachments</h3>
<ul class="attachment-list hide"></ul>
<p class="no-attachments empty">No attachments.</p>
<div class="uploader touch-hide">
<form class="realfile" enctype="multipart/form-data" method="post" action="/api/card/4f701c9daf1765861a15545c">
<input type="hidden" value="" name="token">
<input type="hidden" value="attachFile" name="method">
<input type="hidden" value="http" name="format">
<input class="js-attach-file" type="file" tabindex="-1" name="upload">
</form>
<a class="button-link highlight-icon fakefile js-fakefile">
</div>
<p class="error js-file-too-large" style="display:none; -moz-border-radius: 3px; -webkit-border-radius: 3px; font-weight: bold; margin: 8px 0 0; padding: 6px; background: #e3e3e3; ">File too large. 10mb limit.</p>
</div>
<div class="window-module other-actions clearfix">
<div class="window-module quiet-actions clearfix">
</div>
</div>
</div>
</div>
</div>
<div class="pop-over clearfix fancy-scrollbar" style="display: none;">
<script>
<script src="https://d2k1ftgv7pobq7.cloudfront.net/js/78dcaf5810779a263fe6939fc4895487/all.js">
</body>
</html>
Ramesh
  • 33
  • 2
  • 8
  • Have you tried "watching" the selenium process and checked whats really happening? – Christoph Fink Mar 26 '12 at 07:38
  • Yes I tried ...but actually out put was nothing.......I was not able to understand what selenium is doing actually........In the source code get by selenium .......the upload button was available...let me know if you want more information.........Thanks . – Ramesh Mar 26 '12 at 09:18
  • What EXACTLY does selenium do or not do? Does it enter the path? Does it start the upload? Does it gives any error in the selenium rc? – Christoph Fink Mar 26 '12 at 09:36
  • .you can see that only one button "Upload" is visible ...there is no field to send keys .......and that upload button is doing two actions ....one is opening the window to select file and then doing action to submit the file........I think now you can got what I am trying to do..... – Ramesh Mar 27 '12 at 12:38

3 Answers3

0

You can find solution in below link. You can solve it by using javascript

  String script = "document.getElementById('fileName').value='" +"C:\\\\temp\\\\file.txt" + "';";
  ((IJavascriptExecutor)driver).executeScript(script);

Webdriver: File Upload

Community
  • 1
  • 1
-1

selenium is not able to identify or recognize the element whose attribute is type file.

Like example

 <input type="file" id="id1" name="abc"></input>

so, to handle this situation we have to use third party tool like Point Position to compute x & Y coordinate of browse button.

then we can use either Low level mouse click handle in c# or we have to Use Autoit V3 tool to handle window popup.

You can find more in detail with full explanation and Practical example on my blog: http://avinashpandeblogsonseleniumautomation.blogspot.in/2015/06/upload-file-using-selenium-web-driver.html

Avinash Pande
  • 1,510
  • 19
  • 17
-3

If you are using Firefox browser instead of Send keys use type.

And If the browser is IE then u have to use tool like Autoit Or Selenium 2 contains a method attach_file or use this

sel.attach_file("css=input[type=file]", "http://url.com/file.txt")

lAH2iV
  • 1,159
  • 2
  • 12
  • 28
  • Hi Thanks.........I tried this also....but I think I have some different controls.......instead of 1 test box ..where I can put the path of file and second is to submit and send the request to server. I have only one button which is doing two actions .one is to open the window to select file and other is to send file upload request to server. – Ramesh Mar 27 '12 at 12:43
  • The original question is referring to Selenium WebDriver. The methods you refer to do not exist in Selenium WebDriver, but rather Selenium RC. – JimEvans Mar 28 '12 at 15:24