在测试过程中,文件(.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;
1条答案
按热度按时间iqih9akk1#
尝试将“headless”选项更改为“--headless=new”
这一点最近在Chrome 109中有所改变
https://www.selenium.dev/blog/2023/headless-is-going-away/
https://developer.chrome.com/articles/new-headless/