运行 使用 xvfb 在无头模式下进行测试时无法使用 sendkeys() 上传图像文件?

Unable to upload image file using sendkeys() when running tests in headless mode using xvfb?

我想在这里做的是在 Linux 机器上的无头浏览器 Firefox 中使用 sendKeys() 上传图像文件(仅供参考:测试在真实浏览器中运行良好 FF:42, 硒 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>
</div>

如果您是 运行 远程实例,您可能需要设置文件检测器:

driver.setFileDetector(new LocalFileDetector());

WebElement element = driver.findElement(By.cssSelector("input[name='file']");
element.sendKeys("Path to file");

对我有用的解决方案是:

在 ubuntu 机器上更新到最新版本的 firefox 解决了这个问题,无论如何感谢@Florant B 的支持。