【问题标题】:Selenium upload file: file not found [docker]Selenium 上传文件:找不到文件 [docker]
【发布时间】:2019-07-15 13:38:57
【问题描述】:

我有以下使用硒上传图像的方法。

public static void uploadSampleImage(StaticSeleniumDriver driver)
{
    File file = new File(System.getProperty("user.dir") + "/resources/images/" + SAMPLE_DOCUMENT_FILE_NAME);
    Utils.Log("file exists: " + file.exists());

    String imagePath = file.getAbsolutePath();
    WebElement input = driver.findElement(By.name("file"));
    input.sendKeys(imagePath);
}

这是提供文件路径 (like explained in Guru99 tutorial) 以上传文件的标准方式。

  1. 在 windows 上本地测试时效果很好
  2. 在 docker 容器 (linux) 中运行时无法正常工作,出现以下错误:

org.openqa.selenium.InvalidArgumentException:无效参数:文件 未找到:/usr/src/app/resources/images/image2.png (会话信息:chrome=72.0.3626.81) (驱动信息: chromedriver=2.46.628388 (4a34a70827ac54148e092aafb70504c4ea7ae926),platform=Linux 4.9.125-linuxkit x86_64)(警告:服务器未提供任何堆栈跟踪信息)

这很奇怪,因为我确定给定目录中存在文件(在我上面的方法中,我正在检查文件是否存在并且日志清楚地确认了这一点)

欢迎提出建议,谢谢

【问题讨论】:

    标签: java selenium docker


    【解决方案1】:

    对于RemoteWebDriver,您必须设置文件检测器driver.setFileDetector(new LocalFileDetector());。 你的代码:

    public static void uploadSampleImage(StaticSeleniumDriver driver)
    {
        driver.setFileDetector(new LocalFileDetector());
        File file = new File(System.getProperty("user.dir") + "/resources/images/" + SAMPLE_DOCUMENT_FILE_NAME);
        Utils.Log("file exists: " + file.exists());
    
        String imagePath = file.getAbsolutePath();
        WebElement input = driver.findElement(By.name("file"));
        input.sendKeys(imagePath);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-30
      • 2021-08-18
      • 2017-06-16
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      • 1970-01-01
      相关资源
      最近更新 更多