简体   繁体   中英

Upload a file using selenium web driver in c#

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>

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

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

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM