我想自动化文件上传过程,该过程使用 bootstrap 内置的文件上传控件。
我正在使用webdriver做同样的事情。
以下是我的代码,但不幸的是它无法正常工作:
element=driver.findElement(By.xpath("//[@id='upload']/fieldset/div[2]/input[1]"));
element.sendKeys(pathToFile);
它给出了
element not visible
错误。这是我要自动执行的bootstrap fileupload控件的示例-
通过JavaScript:
在此URL上http://markusslima.github.io/bootstrap-filestyle/
请看下面的样式-
$(":file").filestyle({icon: false});
最佳答案
好的。我想我解决了。
WebElement fileInput = driver.findElement(By.id("document"));
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.id("document"));
js.executeScript("arguments[0].setAttribute('style', 'left:30px')",
element);
fileInput.sendKeys(fileName);
bootstrap-filestyle.js隐藏了输入元素,因此您必须将其移动到可见区域,然后以标准方式进行设置。
这么简单的解决方案带来了很多麻烦。
这是我原来的html代码:
<span id="documentUpload">
<input type="file" id="document" name="document" class="notMandatory" onkeypress="return noenter(event)" tabindex="-1" style="position: absolute; left: -9999px;">
<div class="bootstrap-filestyle" style="display: inline;" tabindex="0">
<input type="text" class="input-xlarge" disabled="" autocomplete="off">
<label for="document" class="btn"><i class="icon-folder-open"></i> <span>Upload</span></label>
</div>
</span>
关于file-upload - 如何使用WebDriver自动执行引导文件上传上载控制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29271706/