简体   繁体   English

Selenium 上传文件:找不到文件[docker]

[英]Selenium upload file: file not found [docker]

I have following method that uploads image using selenium.我有以下使用 selenium 上传图像的方法。

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);
}

That's a standard way of feeding file path ( like explained in Guru99 tutorial ) to upload file.这是提供文件路径( 如 Guru99 教程中所述)以上传文件的标准方式。

  1. It works fine when testing locally on windows在 windows 上本地测试时它工作正常
  2. It is NOT working when run inside docker container (linux), getting this error:在 docker 容器 (linux) 中运行时它不工作,出现此错误:

org.openqa.selenium.InvalidArgumentException: invalid argument: File not found: /usr/src/app/resources/images/image2.png (Session info: chrome=72.0.3626.81) (Driver info: chromedriver=2.46.628388 (4a34a70827ac54148e092aafb70504c4ea7ae926),platform=Linux 4.9.125-linuxkit x86_64) (WARNING: The server did not provide any stacktrace information) org.openqa.selenium.InvalidArgumentException:参数无效:找不到文件:/usr/src/app/resources/images/image2.png(会话信息:chrome=72.0.3626.81)(驱动程序信息:chromedriver=2.46.628388(4a34a70827ac54148e092aafbea ),platform=Linux 4.9.125-linuxkit x86_64)(警告:服务器未提供任何堆栈跟踪信息)

Which is weird because I am sure file exist in given directory (in my method above, I am checking if file exists and log clearly confirms that)这很奇怪,因为我确定文件存在于给定目录中(在我上面的方法中,我正在检查文件是否存在并且日志清楚地确认了这一点)

在此处输入图像描述

Any suggestions would be welcome, thank you欢迎任何建议,谢谢

For RemoteWebDriver you have to set file detector driver.setFileDetector(new LocalFileDetector());对于RemoteWebDriver您必须设置文件检测器driver.setFileDetector(new LocalFileDetector()); . . Your code:您的代码:

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);
}

Instead of using '/' in your path string, you can use File.separator which takes care of the OS level file separator automatically under the hood.您可以使用File.separator而不是在路径字符串中使用“/”,它会在后台自动处理操作系统级别的文件分隔符。 Using this, your code becomes independent of any OS, and it lets Java take care of what separator to use as per the OS instead of you worrying about it.使用这个,你的代码变得独立于任何操作系统,它让 Java 根据操作系统来处理使用什么分隔符,而不是你担心它。

So the first line of code becomes:所以第一行代码变成:

new File(System.getProperty("user.dir") + File.separator + "resources" + File.separator + "images" + File.separator + SAMPLE_DOCUMENT_FILE_NAME);

and rest of the line remains same.该行的 rest 保持不变。

.! .! No extra headaches.没有额外的头痛。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM