问题描述
在下面的代码中,我们试图在 chrome 打开文件时捕获信息,但在某些情况下它会失败,因为某些日志文件正在下载并且在 chrome 中无法查看。
如果必须添加任何首选项,以便可以查看或下载所有文件(无论大小),请告诉我。
public class DownloadFile {
String FILE_ADD = "file:///";
String LOG_PATH = "path";
String LOG_FILE = "sample.log";
String OUTPUT_FILE = "output.txt";
public static RemoteWebDriver driver;
@BeforeEach
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver","chromedriver.exe");
ChromeOptions options = new ChromeOptions();
//Map<String,Object> prefs = new HashMap<String,Object>();
//prefs.put("download.prompt_for_download",false);
//prefs.put("log.disabled",true);
//options.setExperimentalOption("prefs",prefs);
//driver = new ChromeDriver(options);
//driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
//driver.manage().window().maximize();
String fileDownloadpath = "downnloadpath";
Map<String,Object> prefsMap = new HashMap<String,Object>();
prefsMap.put("profile.default_content_settings.popups",0);
prefsMap.put("download.default_directory",fileDownloadpath);
prefsMap.put("txtjs.disabled",true);
ChromeOptions option = new ChromeOptions();
option.setExperimentalOption("prefs",prefsMap);
option.addArguments("--test-type");
option.addArguments("--disable-extensions");
//Any preference to be added to download all the files instead of viewing some files in chrome
driver = new ChromeDriver(option);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.manage().window().maximize();
}
@Test
public void fileDownload() throws AWTException,InterruptedException,IOException {
driver.get(FILE_ADD+LOG_PATH+LOG_FILE);
//String output = driver.findElement(By.xpath("/html/body/pre")).getText();
//String final_output = output.replaceAll("User.*>","testing");
//FileUtils.writeFile(LOG_PATH+OUTPUT_FILE,output);
Thread.sleep(7000);
}
@AfterEach
public void tearDown() throws Exception {
driver.quit();
}
}
解决方法
据我所知,您不能拥有此自定义,您可以将其设置为查看 1 个文件或下载下一个文件,除非您覆盖下载下方的 ChromeDriver 选项...
下面是如果你想下载文件/没有对话框:
download.prompt_for_download 是您应该使用的。
String downloadFilepath = "C:\\DownloadDirectoryPath"
HashMap<String,Object> chromePreferences = new HashMap<String,Object>();
chromePreferences.put("profile.default_content_settings.popups",0);
chromePreferences.put("download.prompt_for_download","false");
chromePreferences.put("download.default_directory",downloadFilepath);
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("prefs",chromePreferences);
=== 编辑 ===
您可以尝试使用该选项:
chromePreferences.put("profile.content_settings.exceptions.automatic_downloads.*.setting",1 );
你在使用 setExperimentalOption 吗?您的 ChromeDriver 版本是什么?
使用:ChromeDriver 91,您可以查看目录下的首选项文件:C:\Users
以下网址,Chrome 不会弹出下载对话框。
在我的首选项文件中,我以以下内容为例:
"automatic_downloads": {
"https://folkets-lexikon.csc.kth.se:443,*": {
"last_modified": "13182517278621031","setting": 1
},"https://mail.google.com:443,*": {
"last_modified": "13160578764124505","setting": 1
}
},
作为参考,以下内容很有帮助:
Disable chrome download multiple files confirmation
How to use chrome webdriver in selenium to download files in python?