gpt4 book ai didi

java - 使用 chrome headless 浏览器时无法下载文件

转载 作者:行者123 更新时间:2023-12-02 02:35:35 24 4
gpt4 key购买 nike

我正在使用 headless Chrome 驱动程序。单击文件下载选项后,我在所需位置看不到任何文件。但是,在没有 headless 的情况下,下载工作正常。请找到我正在使用的 ChromeOptions 和 DesiredCapability

DesiredCapabilities caps = DesiredCapabilities.chrome();

Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.notifications", 2);
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("download.default_directory", System.getProperty("user.dir")+File.separator+"download");

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless", "window-size=1024,768", "--no-sandbox");
chromeOptions.addArguments("--disable-dev-shm-usage");
chromeOptions.setExperimentalOption("prefs", prefs);
chromeOptions.addArguments("disable-popup-blocking");
chromeOptions.addArguments("--disable-extensions");
caps.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

最佳答案

为您快速谷歌一下,结果是https://bugs.chromium.org/p/chromium/issues/detail?id=696481建议使用

Page.setDownloadBehavior。类似于(如下):免责声明:逐字复制

                    System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
options.addArguments("--headless");
options.addArguments("--disable-extensions"); //to disable browser extension popup

ChromeDriverService driverService = ChromeDriverService.createDefaultService();
ChromeDriver driver = new ChromeDriver(driverService, options);

Map<String, Object> commandParams = new HashMap<>();
commandParams.put("cmd", "Page.setDownloadBehavior");
Map<String, String> params = new HashMap<>();
params.put("behavior", "allow");
params.put("downloadPath", "//home//vaibhav//Desktop");
commandParams.put("params", params);
ObjectMapper objectMapper = new ObjectMapper();
HttpClient httpClient = HttpClientBuilder.create().build();
String command = objectMapper.writeValueAsString(commandParams);
String u = driverService.getUrl().toString() + "/session/" + driver.getSessionId() + "/chromium/send_command";
HttpPost request = new HttpPost(u);
request.addHeader("content-type", "application/json");
request.setEntity(new StringEntity(command));
httpClient.execute(request);
driver.get("http://www.seleniumhq.org/download/");
driver.findElement(By.linkText("32 bit Windows IE")).click();

关于java - 使用 chrome headless 浏览器时无法下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57204471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com