2016-03-03 71 views
0

我想用selenium web驅動程序上傳文件。用戶界面有一個div標籤內的上傳按鈕。該按鈕具有位於span標籤內的隱藏文本框。我可以找到span標籤。找到輸入元素併發送文件路徑來上傳文件

<div class="ng-scope" ng-if="showUpload" style="padding-bottom:5px;"> 
<div class="btn ng-binding" ng-class="class" flow-drop="" style="padding-top:30px;vertical-align:middle;width:370px;height:100px;background-color:#eee;border:1px solid #ccc" ng-style="style" flow-drag-leave="style={border:'1px solid #ccc'}" flow-drag-enter="style={border:'1px solid #007670'}" flow-prevent-drop="">Drag and Drop your files here.</div> 
<span class="ng-binding" style="padding:0 20px">or</span> 
<span class="btn btn-primary ng-binding" flow-attrs="{accept:'image/*,.pdf,.csv,.txt,.doc'}" flow-btn=""> 
<i class="icon icon-file"/> 
Upload File      
<input type="file" style="visibility: hidden; position: absolute; width: 1px; height: 1px;" multiple="multiple" accept="image/*,.pdf,.csv,.txt,.doc"/> 
</span> 
</div> 

我能夠通過firepath找到輸入標記,但使用java代碼的相同工作不起作用。

WebElement m_upload = driver.findElement(By.CssSelector("input[type='files']"));返回null。 任何人都可以告訴我如何去做這件事?

+0

你檢查文件單數與文件複數? –

+0

我無法找到輸入文本框。只有這樣我才能使用sendKeys()發送文件。 – Ansu

+0

引起:org.openqa.selenium.NotFoundException:在VISIBLE狀態中找不到與'By.cssSelector:input [type ='files']'匹配的元素。 – Ansu

回答

0

試試下面的代碼

//Give your file path here 
    var orgFile = @"C:\Users\username\Downloads\pdf.pdf"; 
//get uploadfile input and send pat using SendKeys 
      var uploadFile = driver.FindElement(By.CssSelector("input[type='file']")); 
      uploadFile.SendKeys(orgFile); 

如有問題,然後讓我知道。

0

您無法對隱藏元素執行任何操作。首先,您必須執行一個操作來更改該隱藏的輸入元素的可見性。

另外請記住,頁面使用的是AngularJS,並且您正在使用Selenium進行測試,因此可能存在同步問題,因此更好的方法是使用量角器對其進行測試,或者通過將Implicit或Explicit等待語句在上傳文件之前。

我認爲它會解決問題。

+0

沒有沒有工作。 – Ansu

相關問題