我有以下使用硒上传图像的方法。
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);
}
这是提供文件路径的标准方法(就像 Guru99 教程中解释的那样 https://www.guru99.com/upload-download-file-selenium-webdriver.html) 上传文件。
- 在windows本地测试时效果很好
- 在 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)(警告:服务器未提供任何堆栈跟踪信息)
这很奇怪,因为我确定文件存在于给定目录中(在上面的方法中,我正在检查文件是否存在并且日志清楚地证实了这一点)
欢迎任何建议,谢谢
For 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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)