我在这里要做的是使用sendKeys()在linux机器的无头浏览器Firefox中上传图像文件(仅供参考:测试在真实浏览器中运行良好FF:42,selenium 2.47.2)
```
WebElement element = driver.findElement(By.cssSelector("input[name='file']");
element.sendKeys("Path to file");
```
我也在这里添加HTML代码:
```
<div class="form-group col-lg-8 col-md-8 col-sm-7" data-content="image-creative">
<div class="form-group" data-content="image-creative-upload">
<div class="upload-image-field">
<label for="uploadImage">Upload an image in jpg, png or gif format</label>
<div class="fileinput fileinput-new input-group" data-provides="fileinput">
<div class="form-control" data-trigger="fileinput">
<i class="glyphicon glyphicon-file fileinput-exists"></i>
<span class="fileinput-filename"></span>
</div>
<span class="input-group-addon btn btn-default btn-file">
<span class="fileinput-new">Select file</span>
<span class="fileinput-exists">Change</span>
<input id="uploadImage" class="file" type="file" placeholder="No image uploaded" accept="image/*" name="file">
</span>
<a class="input-group-addon btn btn-default fileinput-exists" data-dismiss="fileinput" href="#">Remove</a>
</div>
<p class="text-danger upload-error hidden">
</div>
</div>
```
任何帮助?非常感谢:))
答案 0 :(得分:1)
如果您正在运行远程实例,则可能需要设置文件检测器:
driver.setFileDetector(new LocalFileDetector());
WebElement element = driver.findElement(By.cssSelector("input[name='file']");
element.sendKeys("Path to file");
答案 1 :(得分:0)
对我有用的解决方案是:
在ubuntu机器上更新到firefox的最新版本是一个问题,无论如何,感谢@Florant B的支持。