2012-03-26 84 views
1

我想在c#中使用硒web驱动程序上传文件。 (“C:\ Users \ test \ Desktop \ test.cs”);如果你想要使用它,你可以使用下面的代码:在c#中使用硒web驱动程序上传文件#

但这不起作用。

这在硒的webdriver正确硒IDE,但不工作。

以下是网页的HTML代码:

,我想使用下列元素:如果您使用的是Firefox浏览器,而不是Send keys使用type <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> 
+0

解决这个问题,你是否尝试“看”硒进程,并检查什么真的发生了? – ChrFin 2012-03-26 07:38:51

+0

是的,我尝试过......但实际上没有东西.......我无法理解硒在做什么........在源代码中获取硒...... ...上传按钮可用...让我知道你是否想要更多的信息.​​........谢谢。 – Ramesh 2012-03-26 09:18:31

+0

硒究竟做了什么或不做什么?它是否进入路径?它是否开始上传?它是否会在硒rc中发生任何错误? – ChrFin 2012-03-26 09:36:55

回答

-3

以及如果所述浏览器是IE然后u必须使用工具等的AutoIt或硒2包含方法attach_file或使用this

sel.attach_file( “CSS =输入[类型=文件]”,“HTTP: //url.com/file.txt“)

+0

嗨,谢谢.........我也试过这个....但我想我有一些不同的控件.......而不是1个测试框..其中我可以把文件的路径,其次是提交并发送请求到服务器。我只有一个按钮正在做两个动作,一个是打开窗口选择文件,另一个是发送文件上传请求到服务器。 – Ramesh 2012-03-27 12:43:00

+0

原来的问题是指Selenium WebDriver。您引用的方法在Selenium WebDriver中不存在,而是Selenium RC。 – JimEvans 2012-03-28 15:24:42

-1

硒是不能识别或识别其属性的类型文件中的元素。

Like example 

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

因此,处理这种情况,我们必须使用第三方工具,如点位置,计算浏览按钮的X & Y坐标。

那么我们可以在C#中使用或者低级别鼠标点击手柄或我们不得不使用了AutoIt V3工具来处理窗口弹出。

你可以找到更多详细的我的博客上充分的解释和实例:http://avinashpandeblogsonseleniumautomation.blogspot.in/2015/06/upload-file-using-selenium-web-driver.html

0

你可以在以下链接解决方案。 您可以使用JavaScript

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

Webdriver: File Upload

相关问题