嗨我想知道如果没有ID字段我是否可以使用此命令,似乎按钮接受输入。我试过.sendKeys但它似乎没有做任何事情,地方说不要使用.click();命令后。
这是按钮的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 :(得分: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);
我认为该页面上唯一的文件输入标记未隐藏。如果不是,您可能需要调整文件选择器