文件未下载到chrome-headless和selenium java中指定的下载目录中

qgelzfjb  于 2023-03-06  发布在  Go
关注(0)|答案(1)|浏览(202)

在测试过程中,文件(.pdf)将从Web应用程序下载,我必须验证该文件是否下载到特定路径下。但每当我进入headless模式时,文件未下载到下载路径(即指向“download.default_directory”),而是下载到“user.dir”('/home/〈〉/git/bb-cloud/〈〉)。Chrome版本:110.0.5481.100的规定;操作系统:Ubuntu 20.04下面是我的代码,用于调用chrome-headless,

public WebDriver createInstance(String browser) {
WebDriver driverInstance = null;
try {
  if (browser.equalsIgnoreCase("chrome-headless")) {
    WebDriverManager.chromedriver().setup();
    ChromeOptions options = new ChromeOptions();

    HashMap < String, Object > hashmap = new HashMap < > ();
    hashmap.put("profile.default_content_settings.popups", 0);
    hashmap.put("download.prompt_for_download", "false");
    hashmap.put("download.default_directory", '/home/<<USER>>/git/bb-cloud/<<PROJECT>>/src/main/resources');
    hashmap.put("browser.set_download_behavior", "{ behavior: 'allow' , downloadPath: '/home/<<USER>>/git/bb-cloud/<<PROJECT>>/src/main/resources'}");

    options.setExperimentalOption("prefs", hashmap);

    options.addArguments("start-maximized");
    options.addArguments("window-size=1920,1080");

    options.addArguments("headless");
    options.addArguments("--no-sandbox");
    options.addArguments("--disable-gpu");
    options.addArguments("--disable-dev-shm-usage");
    options.addArguments("--allow-insecure-localhost");

    options.setAcceptInsecureCerts(true);
    driverInstance = new ChromeDriver(options);
  }
} catch (Exception e) {
  e.printStackTrace();
}
return driverInstance;

相关问题