Nih*_*vel 5 java selenium google-chrome download
我无法让 Selenium 和 Chrome (Canary) 下载文件。我正在使用 Java 和 Chrome 59/60(因为我的测试适用于 Windows 和 Linux),并且我正在尝试开始从网页下载文件。
当我从 selenium 中不设置无头模式时,chrome 窗口将打开并下载文件。
当我设置标志时--headless,chrome 窗口不会打开,下载也不会开始。
public static void chromeDownload() throws IOException, InterruptedException{
ChromeOptions options = new ChromeOptions();
String downloadFilepath = "";
if (ValidateOS.isWindows()){
System.out.println("This is a Windows system.");
System.setProperty("webdriver.chrome.driver", "resources\\driver\\chromedriver.exe");
options.setBinary("C:\\Users\\Juri\\AppData\\Local\\Google\\Chrome SxS\\Application\\chrome.exe");
downloadFilepath = "C:\\";
} else if (ValidateOS.isUnix()){
System.out.println("This is a Unix system.");
System.setProperty("webdriver.chrome.driver", "resources/driver/chromedriver");
options.setBinary("/usr/bin/google-chrome");
downloadFilepath = "/home/juri/";
}
// Manage the download
HashMap<String, Object> chromePrefs = new HashMap<>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
// Save Chrome Options
HashMap<String, Object> chromeOptionsMap = new HashMap<>();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--headless --disable-gpu");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(cap);
driver.get("http://localhost/my-test-page.html");
driver.findElement(By.id("download")).click();
Thread.sleep(5000); // wait 5 seconds for a small file to download.. yes.. I know...
driver.quit();
}
Run Code Online (Sandbox Code Playgroud)
单击 后,在 GUI 模式下开始下载。在无头模式下,则不会。
怎么解决?
我正在使用 Chrome Canary,它的 v.60 带有--headless功能。在没有 GUI 的服务器上运行抓取器非常方便。但是,出于同样的原因......我发现在没有 GUI 的服务器上下载 Chrome 是没有用的。除了主要问题之外.. 我想知道开发者们是否认为在 Linux 服务器上安装 chrome 只是为了以无头模式启动它是可以的。
更新:如果有人读过此内容,我仍在寻找解决方案:/搜索结果有一些,我尝试了所有这些
通过调整此链接中找到的代码来解决: 使用 ChromeDriver 和无头模式下载 Java、Selenium 中的文件
对于那些想知道我的代码现在怎么样的人......
public static void chromeDownload(String address, String Headless, String DownDir) throws IOException, InterruptedException{
ChromeOptions options = new ChromeOptions();
String downloadFilepath = DownDir;
if (ValidateOS.isWindows()){
System.out.println("This is a Windows system.");
System.setProperty("webdriver.chrome.driver", "resources\\driver\\chromedriver.exe");
//options.setBinary("C:\\Users\\Juri\\AppData\\Local\\Google\\Chrome SxS\\Application\\chrome.exe");
// If this is commented, the grabber will use the main Chrome
} else if (ValidateOS.isUnix()){
System.out.println("This is a Unix system.");
System.setProperty("webdriver.chrome.driver", "resources/driver/chromedriver");
options.setBinary("/usr/bin/google-chrome");
}
switch (Headless.toUpperCase()){
case "TRUE":
options.addArguments("--headless --disable-gpu");
break;
case "FALSE":
default:
options.addArguments("--window-size=1152,768");
break;
}
options.addArguments("--test-type");
options.addArguments("--disable-extension");
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", downloadFilepath);
params.put("cmd", "Page.setDownloadBehavior");
commandParams.put("params", params);
ObjectMapper objectMapper = new ObjectMapper();
CloseableHttpClient 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(address);
driver.findElement(By.id("download")).click();
driver.quit();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5747 次 |
| 最近记录: |