简体   繁体   English

在C#中使用Selenium Web驱动程序上传文件

[英]Upload a file using selenium web driver in c#

I am trying to upload a file using selenium web driver in c#. 我正在尝试使用C#中的Selenium Web驱动程序上传文件。

I am trying this: WebDriver.FindElement(By.Name("upload")).SendKeys("C:\\Users\\test\\Desktop\\test.cs"); 我正在尝试: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. 这在selenium IDE中可以正常工作,但在selenium webdriver中不能正常工作。

Following is the HTML code of the page: 以下是页面的HTML代码:

where I am trying to use following element: <input class="js-attach-file" type="file" tabindex="-1" name="upload"> 我尝试使用以下元素的地方: <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 您可以使用javascript来解决

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

Webdriver: File Upload Webdriver:文件上传

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. 因此,要处理这种情况,我们必须使用诸如Point Position之类的第三方工具来计算浏览按钮的x和Y坐标。

then we can use either Low level mouse click handle in c# or we have to Use Autoit V3 tool to handle window popup. 那么我们可以在c#中使用低级鼠标单击手柄,也可以使用Autoit V3工具来处理窗口弹出。

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 您可以在我的博客上找到具有详细说明和实际示例的更多详细信息: 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 . 如果您使用的是Firefox浏览器而不是Send keys使用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 如果浏览器是IE,则您必须使用Autoit或Selenium 2之类的工具,其中包含方法attach_file或使用方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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