2015-04-17 21 views
2

嗨我想知道如果我沒有ID字段時可以使用此命令,它似乎像按鈕接受輸入。我試過.sendKeys,但它似乎沒有做任何事情,地方說不使用.click();命令後。上傳文件沒有ID字段的web驅動程序sendKey

這裏是按鈕的html。

<button class="md-button-icon mt-toolbar-button md-button md-default-theme ng-pristine ng-untouched ng-valid" accept="image/*" ng-model="chatFiles" ng-disabled="!sessionStarted" ng-show="!chatMessage" ng-file-select="" tabindex="0" style="overflow: hidden;" aria-hidden="false" aria-invalid="false" aria-disabled="false"> 
    <mt-icon class="ng-isolate-scope" height="24" width="24" icon=" attachment" style="width: 24px; height: 24px;"> 
    <span>menu</span> 
    <input type="file" accept="image/*" style="width: 1px; height: 1px; opacity: 0; position: absolute; padding: 0px; margin: 0px; overflow: hidden;" tabindex="-1" ng-file-generated-elem="true"> 
</button> 

這是我的webdriver代碼:

wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[2]/div/md-card/md-content/div/div[2]/button[2]"))); 
WebElement Upload = driver.findElement(By.xpath("/html/body/div[2]/div/md-card/md-content/div/div[2]/button[2]")); 
Upload.sendKeys("C:/Users/elsid/Desktop/Eclipse/Workspace/NG - Mentored/Autoit/test.png"); 

這是從上傳圖片的代碼一路下跌到按鈕一次。

<li class="ng-scope mt-chat-mine" layout="row" ng-class="{ 'mt-chat-mine': message.isMine()}" ng-repeat="message in messages"> 
     <!-- 

     ngIf: !message.isMine() 

     --> 
     <!-- 

     ngIf: message.type == 1 

     --> 
     <!-- 

     ngIf: message.type == 2 

     --> 
     <img class="chat-image ng-scope" ng-if="message.type == 2" ng-src="http://mentoredapp.perrysysdev.com/usercontent/SessionFiles/8040eeaa-3584-4a13-b54e-a16bfadd6433.jpg" src="http://mentoredapp.perrysysdev.com/usercontent/SessionFiles/8040eeaa-3584-4a13-b54e-a16bfadd6433.jpg"></img> 
     <!-- 

     end ngIf: message.type == 2 

     --> 
     <!-- 

     ngIf: message.type == 3 

     --> 
     <!-- 

     ngIf: message.isMine() 

     --> 
     <img class="avatar ng-scope" ng-if="message.isMine()" ng-src="http://mentoredapp.perrysysdev.com/usercontent/TutorProfilePictures/25f586a2-5031-49a5-94a3-e65305faed8a.png" src="http://mentoredapp.perrysysdev.com/usercontent/TutorProfilePictures/25f586a2-5031-49a5-94a3-e65305faed8a.png"></img> 
     <!-- 

     end ngIf: message.isMine() 

     --> 
    </li> 
    <!-- 

    end ngRepeat: message in messages 

    --> 

</ul> 
<div class="text-box" layout="row"> 

    <textarea id="chat-message-box" class="message-box ng-pristine ng-untouched ng-valid" placeholder="Send a message.." ng-disabled="!sessionStarted" mt-enter="sendChatMessage()" flex="" ng-model="chatMessage" aria-multiline="true" tabindex="0" aria-invalid="false" aria-disabled="false"></textarea> 

<button class="md-button-icon mt-toolbar-button md-button md-default-theme ng-hide" ng-transclude="" ng-show="chatMessage" ng-disabled="!chatMessage" ng-click="sendChatMessage()" tabindex="0" aria-hidden="true" aria-disabled="true" disabled="disabled"></button> 

<button class="md-button-icon mt-toolbar-button md-button md-default-theme ng-valid ng-dirty ng-valid-parse ng-touched" accept="image/*" ng-model="chatFiles" ng-disabled="!sessionStarted" ng-show="!chatMessage" ng-file-select="" ng-click="clicked" tabindex="0" style="overflow: hidden;" aria-hidden="false" aria-invalid="false" aria-disabled="false"> 

    <mt-icon class="ng-isolate-scope" height="24" width="24" icon=" attachment" style="width: 24px; height: 24px;"></mt-icon> 
    <span></span> 
    <input type="file" accept="image/*" style="width: 1px; height: 1px; opacity: 0; position: absolute; padding: 0px; margin: 0px; overflow: hidden;" tabindex="-1" ng-file-generated-elem="true"></input> 

</button> 
+0

該文件輸入標記是隱藏的嗎? – Saifur

+0

不知道如何檢查?它說隱藏在HTML中,也灰顯 – Elsid

回答

0

可以簡化多的XPath的點點找到元素容易

By xpath = By.xpath("//span[text()='menu']"); 
By fileTag = By.cssSelector("[type='file']"); 

wait.until(ExpectedConditions.presenceOfElementLocated(xpath))); 
WebElement Upload = driver.findElement(fileTag); 
Upload.sendKeys("C:/Users/elsid/Desktop/Eclipse/Workspace/NG - Mentored/Autoit/test.png"); 

如果輸入的文件是隱藏只來處理辦法是JavaScript和設置屬性。試試下面的代碼

String filePath = "C:/Users/elsid/Desktop/Eclipse/Workspace/NG - Mentored/Autoit/test.png"; 
String script = "document.querySelector(\"[type='file']\").setAttribute('value','" + filePath + "');"; 
((JavascriptExecutor)driver).executeScript(script); 

我認爲該網頁上的唯一文件輸入標籤和不是隱藏。如果沒有,您可能需要調整文件選擇器

+0

好的謝謝,我認爲它隱藏說溢出隱藏。我會嘗試一個Java機器人來輸入一切。 – Elsid

+0

你可以檢查第二行,我認爲它有一個語法錯誤,日食是拋出一個錯誤,無效的字符常量。我正在使用Java – Elsid

+0

現在給我錯誤「');」說出語法錯誤,插入「;」完成LocalVariableDeclarationStatement – Elsid

相關問題