我想知道如何使用 Selenium 和 FirefoxDriver将文件上传到bootstrap-fileinput元素。我试过了
WebElement input = letter.findElement(By.cssSelector("#letter input"));
input.sendKeys("/home/me/loremIpsum.pdf");
我明白了
org.openqa.selenium.InvalidArgumentException: File not found: /home/me/loremIpsum.pdf
当然,该文件/home/me/loremIpsum.pdf
确实存在。
相同的代码使用 chromedriver 工作。
我整理了一个 jsfiddle 来显示文件输入按钮:https ://jsfiddle.net/yscgx2zc/
我的应用程序中呈现的 html(从 Firefox 开发者控制台复制)看起来像这样。找到input
靠近底部的元素。
<div id="letter" class="form-group"><label class="control-label">The Letter<span>*</span></label><div class="file-input file-input-new"><div class="file-preview ">
<div class="close fileinput-remove">×</div>
<div class="file-drop-disabled">
<div class="file-preview-thumbnails">
</div>
<div class="clearfix"></div> <div class="file-preview-status text-center text-success"></div>
<div class="kv-fileinput-error file-error-message" style="display: none;"></div>
</div>
</div>
<div class="kv-upload-progress hide"><div class="progress">
<div class="progress-bar progress-bar-success progress-bar-striped active" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width:0%;">
0%
</div>
</div></div>
<div class="input-group file-caption-main">
<div tabindex="500" class="form-control file-caption kv-fileinput-caption">
<div class="file-caption-name"></div>
</div>
<div class="input-group-btn">
<button type="button" tabindex="500" title="Clear selected files" class="btn btn-default fileinput-remove fileinput-remove-button"><i class="glyphicon glyphicon-trash"></i> <span class="hidden-xs">Remove</span></button>
<button type="button" tabindex="500" title="Abort ongoing upload" class="btn btn-default hide fileinput-cancel fileinput-cancel-button"><i class="glyphicon glyphicon-ban-circle"></i> <span class="hidden-xs">Cancel</span></button>
<div tabindex="500" class="btn btn-primary btn-file">
<i class="glyphicon glyphicon-folder-open"></i> <span class="hidden-xs">Browse …</span>
<input data-show-upload="false" data-allowed-file-extensions="["pdf"]" data-allowed-file-types="["pdf"]" accept="application/pdf" class="file" id="1502961793221" type="file"></div>
</div>
</div></div></div>