在新标签页中进行下载时,如何在Selenium(Java)中无头下载文件? [英] How to download files headless in Selenium (Java) when download happens in new tab?
问题描述
我有一个网页,当我单击一个按钮时,它会打开另一个选项卡,然后在其中几秒钟后下载一个csv文件。我试图无休止地自动执行此操作,但无法执行此操作。我正在使用以下代码。但是我认为以下解决方案适用于同一窗口中的下载。我该如何调整它才能在我的情况下正常工作? .addArguments(-headless);
System.setProperty( webdriver.chrome.driver,webdriverpath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption( useAutomationExtension,false);
options.addArguments(-test-type);
options.addArguments(-headless);
options.addArguments(-disable-extensions);
ChromeDriverService driverService = ChromeDriverService.createDefaultService();
HashMap< String,Object> chromePrefs = new HashMap< String,Object>();
chromePrefs.put( profile.default_content_settings.popups,0);
chromePrefs.put( download.default_directory,downloadFilepath);
options.setExperimentalOption( prefs,chromePrefs);
ChromeDriver驱动程序=新的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);
commandParams.put( params,params);
ObjectMapper objectMapper = new ObjectMapper();
HttpClient httpClient = HttpClientBuilder.create()。build();
字符串命令= objectMapper.writeValueAsString(commandParams);
字符串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);
//打开URL
//单击按钮
以下是在Java中适用的解决方案:
单击下载按钮切换到新标签页并刷新:
ArrayList< String> tabs2 = new ArrayList< String> (driver.getWindowHandles());
driver.switchTo()。window(tabs2.get(1));
driver.navigate()。refresh();
(就我而言,它无需刷新即可工作。因此,您也可以尝试不使用该语句)
I have a web page where when I click a button it opens another tab and then in it, downloads a csv file after few seconds. I was trying to automate this headlessly but I am unable to do so. I am using the below code. But I think the below solutions is for download happening in same window. How can I tweak it to work in my situation?
The code works fine and the file gets download if i run this normally (non headless) by commenting out the line options.addArguments("--headless");
System.setProperty("webdriver.chrome.driver", webdriverpath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--test-type");
options.addArguments("--headless");
options.addArguments("--disable-extensions");
ChromeDriverService driverService = ChromeDriverService.createDefaultService();
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
options.setExperimentalOption("prefs", chromePrefs);
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);
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);
//OPEN URL
//Click Button
Here is the solution that worked for in Java :
Right after clicking the download button switch to new tab and also do a refresh :
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
driver.navigate().refresh();
(In my case it worked without the refresh. So you mey try without that statement aswell)
这篇关于在新标签页中进行下载时,如何在Selenium(Java)中无头下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!