从Selenium和chromedriver下载文件 [英] Downloading a file from Selenium and chromedriver
问题描述
我无法让Selenium和Chrome(金丝雀)下载文件。
我使用的是Java和Chrome 59/60(因为我的测试是针对Windows和Linux的),并且我试图从网页开始下载文件。
当我从selenium不设置无头模式时,会打开chrome窗口并下载文件。
设置 - 无头
标志时,chrome窗口不会打开,下载也不会开始。
public static void chromeDownload()throws IOException,InterruptedException {
ChromeOptions options = new ChromeOptions();
String downloadFilepath =;
if(ValidateOS.isWindows()){
System.out.println(这是一个Windows系统。);
System.setProperty(webdriver.chrome.driver,resources \\\\ driver \\chromedriver.exe);
options.setBinary(C:\\ Users\\\\\\\\\\\\\\\\\\\\\\\\ \\chrome.exe);
downloadFilepath =C:\\;
} else if(ValidateOS.isUnix()){
System.out.println(这是一个Unix系统。);
System.setProperty(webdriver.chrome.driver,resources / driver / chromedriver);
options.setBinary(/ usr / bin / google-chrome);
downloadFilepath =/ home / juri /;
}
//管理下载
HashMap< String,Object> chromePrefs = new HashMap<>();
chromePrefs.put(profile.default_content_settings.popups,0);
chromePrefs.put(download.default_directory,downloadFilepath);
//保存Chrome选项
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驱动程序=新的ChromeDriver(cap);
driver.get(http://localhost/my-test-page.html);
driver.findElement(By.id(download))。click();
Thread.sleep(5000); //等待5秒让一个小文件下载..是的..我知道...
driver.quit();
}
在点击开始。在无头模式下,它没有。
如何解决?
OT
我使用的Chrome Canary在v.60版本中提供了 - 无头功能。在没有GUI的服务器上运行抓取器非常方便。
但是,出于同样的原因......我发现在没有GUI的服务器上下载Chrome是没有用的。
除了主要问题之外,我想知道你是否会认为在Linux服务器上安装chrome是为了在无头模式下启动它。
更新:
我仍在寻找一个解决方案,如果有人会读到这个:/搜索结果有几个,我试了一下全部
对于使用ChromeDriver和无头模式的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(这是一个Windows系统。);
System.setProperty(webdriver.chrome.driver,resources \\\\ driver \\chromedriver.exe);
//options.setBinary(\"C:\\Users\\Juri\\AppData\\Local\\Google\\Chrome SxS\\Application\\ \\\chrome.exe);
//如果这是注释,抓取器将使用主要的Chrome
} else if(ValidateOS.isUnix()){
System.out.println(这是一个Unix系统。 );
System.setProperty(webdriver.chrome.driver,resources / driver / chromedriver);
options.setBinary(/ usr / bin / google-chrome);
switch(Headless.toUpperCase()){
caseTRUE:
options.addArguments( - headless --disable-gpu) ;
休息;
caseFALSE:
default:
options.addArguments( - window-size = 1152,768);
休息;
}
options.addArguments( - test-type);
options.addArguments( - disable-extension);
ChromeDriverService driverService = ChromeDriverService.createDefaultService();
ChromeDriver驱动程序=新的ChromeDriver(driverService,options);
地图< 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命令= 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();
}
I can't get Selenium and Chrome (Canary) to download a file. I'm using Java and and Chrome 59/60 (because my tests are both for Windows and Linux) and I'm trying to start the download of a file from a webpage.
When I, from selenium, do NOT set the headless mode, the chrome window opens and the file is downloaded.
When I do set the --headless
flag, the chrome window does not open and the download does not start.
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();
}
At the Click, in GUI mode the download starts. In Headless mode, it doesn't.
How to solve?
OT
I am using Chrome Canary which at its v.60 comes with the --headless feature. Ultra handy for running the grabber on a server without gui. But, for the same reason.. I find it useless to download Chrome on a server without GUI. Beside the main question.. I wonder if you, developers, think that it is okay to install chrome on a Linux server just for starting it in headless mode.
Update: I'm still looking for a solution if someone will ever read this :/ Search results there are a few and I tried them all
Solved by adapting the code found at this link: Download files in Java, Selenium using ChromeDriver and headless mode
For those who wonder how's my code now...
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();
}
这篇关于从Selenium和chromedriver下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!