2012-03-26 36 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

相關問題