在 Mac 上使用 selenium Chromedriver 3.11.0 自动下载文件
Automatically download files using selenium Chromedriver 3.11.0 on a Mac
我已经阅读了这里的一系列 selenium 主题,并且一直在讨论如何为 chromedriver 设置权限/选项。我编写了以下代码:
System.setProperty("webdriver.chrome.driver", "/Users/username/chromedriver");
String downloadFilepath = "//User//username//automation-testing//";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("disable-popup-blocking");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);
下载页面的调用是使用简单的
driver.get(url);
重定向到 csv 文件。
我不断收到弹出提示,询问我是否可以下载文件。值得一提的是,新的 ChromeDriver(cap) 行已被弃用,但我似乎无法找到有关如何使用涵盖此用例的替换它的方法的文档。
看来你快到了。需要使用merge()
from MutableCapabilitiesClass方法将DesiredCapabilities类型对象合并为ChromeOptions类型对象并初始化WebDriver 和 WebClient 实例通过传递 ChromeOptions 对象如下:
System.setProperty("webdriver.chrome.driver", "/Users/username/chromedriver");
String downloadFilepath = "//User//username//automation-testing//";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("disable-popup-blocking");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
options.merge(cap);
WebDriver driver = new ChromeDriver(options);
PS :作为参考,您可以查看 mutablecapabilities
标签中的讨论
更新
根据您的评论更新,您遇到了 下载确认 window,您可以查看讨论 来解决您的问题。
我使用的解决方案是下面非常完整的源代码,通过下面的 google 登录到 jira,然后将过滤器视图下载到下面的 csv(当前选择):
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
public class Scrape {
public static void doScrape(String[] urls) {
try{
for(String url : urls) {
//Create new Chromedriver, set file download path, allow the download popup to be automatically accepted,and merge the properties into chromedriver
System.setProperty("webdriver.chrome.driver", "/Users/damienbell/chromedriver");
String downloadFilepath = "/Users/damienbell/automation-testing";
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
//options.addArguments("--headless");
options.addArguments("--disable-extensions");
//Instantiate above options in driverService
ChromeDriverService driverService = ChromeDriverService.createDefaultService();
ChromeDriver driver = new ChromeDriver(driverService, options);
Map<String, Object> commandParams = new HashMap<>();
commandParams.put("cmd", "Page.setDownloadBehavior");
Map<String, Object> params = new HashMap<String, Object>();
params.put("behavior", "allow");
params.put("downloadPath", downloadFilepath);
params.put("cmd", "Page.setDownloadBehavior");
commandParams.put("params", params);
ObjectMapper om = new ObjectMapper();
CloseableHttpClient httpClient = HttpClients.createDefault();
String command = null;
try{
command = om.writeValueAsString(commandParams);
}catch(JsonProcessingException jpe){ jpe.printStackTrace(); }
String postURL = driverService.getUrl().toString() + "/session/" + driver.getSessionId() + "/chromium/send_command";
HttpPost postRequest = new HttpPost(postURL);
postRequest.addHeader("content-type", "application/json");
postRequest.addHeader("accept", "*.*");
try{
postRequest.setEntity(new StringEntity(command));
httpClient.execute(postRequest);
}
catch (UnsupportedEncodingException uee) { uee.printStackTrace(); }
catch (IOException ioe) { ioe.printStackTrace(); }
driver.get("https://x.atlassian.net/secure/Dashboard.jspa?selectPageId=11502");
Thread.sleep(3000); // Let the user actually see something!
((ChromeDriver) driver).findElementById("menu-sign-in").click();
Thread.sleep(3000);
((ChromeDriver) driver).findElementById("google-signin-button").click();
Thread.sleep(3000);
((ChromeDriver) driver).findElementById("identifierId").sendKeys("email@email.com");
Thread.sleep(700);
((ChromeDriver) driver).findElementById("identifierNext").click();
Thread.sleep(2000);
driver.findElement(By.cssSelector("input[name=password]")).sendKeys(Secret.getPassword());
Thread.sleep(600);
((ChromeDriver) driver).findElementById("passwordNext").click();
Thread.sleep(10000);
driver.get(url);
Thread.sleep(56000);
File[] files = new File("/Users/user/automation-testing").listFiles(new FileFilter() {
@Override
public boolean accept(File path) {
if (path.isFile()) {
ParseCSV.doParse(path);
path.delete();
return true;
}
else{
System.out.println("Failure");
}
return false;
}
});
driver.quit();
}
}catch( java.lang.InterruptedException inter ){ System.err.println("Thread.sleep broke something, wtf"); inter.printStackTrace(); }
}
}
在 python 的 selenium 库中,我向 webdriver.ChromeOptions() 对象添加了“headless”参数,因此它不会显示 chrome window - 表示没有下载提示。
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('headless')
我已经阅读了这里的一系列 selenium 主题,并且一直在讨论如何为 chromedriver 设置权限/选项。我编写了以下代码:
System.setProperty("webdriver.chrome.driver", "/Users/username/chromedriver");
String downloadFilepath = "//User//username//automation-testing//";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("disable-popup-blocking");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);
下载页面的调用是使用简单的
driver.get(url);
重定向到 csv 文件。
我不断收到弹出提示,询问我是否可以下载文件。值得一提的是,新的 ChromeDriver(cap) 行已被弃用,但我似乎无法找到有关如何使用涵盖此用例的替换它的方法的文档。
看来你快到了。需要使用merge()
from MutableCapabilitiesClass方法将DesiredCapabilities类型对象合并为ChromeOptions类型对象并初始化WebDriver 和 WebClient 实例通过传递 ChromeOptions 对象如下:
System.setProperty("webdriver.chrome.driver", "/Users/username/chromedriver");
String downloadFilepath = "//User//username//automation-testing//";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("disable-popup-blocking");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
options.merge(cap);
WebDriver driver = new ChromeDriver(options);
PS :作为参考,您可以查看 mutablecapabilities
标签中的讨论
更新
根据您的评论更新,您遇到了 下载确认 window,您可以查看讨论
我使用的解决方案是下面非常完整的源代码,通过下面的 google 登录到 jira,然后将过滤器视图下载到下面的 csv(当前选择):
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
public class Scrape {
public static void doScrape(String[] urls) {
try{
for(String url : urls) {
//Create new Chromedriver, set file download path, allow the download popup to be automatically accepted,and merge the properties into chromedriver
System.setProperty("webdriver.chrome.driver", "/Users/damienbell/chromedriver");
String downloadFilepath = "/Users/damienbell/automation-testing";
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
//options.addArguments("--headless");
options.addArguments("--disable-extensions");
//Instantiate above options in driverService
ChromeDriverService driverService = ChromeDriverService.createDefaultService();
ChromeDriver driver = new ChromeDriver(driverService, options);
Map<String, Object> commandParams = new HashMap<>();
commandParams.put("cmd", "Page.setDownloadBehavior");
Map<String, Object> params = new HashMap<String, Object>();
params.put("behavior", "allow");
params.put("downloadPath", downloadFilepath);
params.put("cmd", "Page.setDownloadBehavior");
commandParams.put("params", params);
ObjectMapper om = new ObjectMapper();
CloseableHttpClient httpClient = HttpClients.createDefault();
String command = null;
try{
command = om.writeValueAsString(commandParams);
}catch(JsonProcessingException jpe){ jpe.printStackTrace(); }
String postURL = driverService.getUrl().toString() + "/session/" + driver.getSessionId() + "/chromium/send_command";
HttpPost postRequest = new HttpPost(postURL);
postRequest.addHeader("content-type", "application/json");
postRequest.addHeader("accept", "*.*");
try{
postRequest.setEntity(new StringEntity(command));
httpClient.execute(postRequest);
}
catch (UnsupportedEncodingException uee) { uee.printStackTrace(); }
catch (IOException ioe) { ioe.printStackTrace(); }
driver.get("https://x.atlassian.net/secure/Dashboard.jspa?selectPageId=11502");
Thread.sleep(3000); // Let the user actually see something!
((ChromeDriver) driver).findElementById("menu-sign-in").click();
Thread.sleep(3000);
((ChromeDriver) driver).findElementById("google-signin-button").click();
Thread.sleep(3000);
((ChromeDriver) driver).findElementById("identifierId").sendKeys("email@email.com");
Thread.sleep(700);
((ChromeDriver) driver).findElementById("identifierNext").click();
Thread.sleep(2000);
driver.findElement(By.cssSelector("input[name=password]")).sendKeys(Secret.getPassword());
Thread.sleep(600);
((ChromeDriver) driver).findElementById("passwordNext").click();
Thread.sleep(10000);
driver.get(url);
Thread.sleep(56000);
File[] files = new File("/Users/user/automation-testing").listFiles(new FileFilter() {
@Override
public boolean accept(File path) {
if (path.isFile()) {
ParseCSV.doParse(path);
path.delete();
return true;
}
else{
System.out.println("Failure");
}
return false;
}
});
driver.quit();
}
}catch( java.lang.InterruptedException inter ){ System.err.println("Thread.sleep broke something, wtf"); inter.printStackTrace(); }
}
}
在 python 的 selenium 库中,我向 webdriver.ChromeOptions() 对象添加了“headless”参数,因此它不会显示 chrome window - 表示没有下载提示。
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('headless')