简体   繁体   English

Chrome会继续在新标签页中打开pdf文件

[英]Chrome keeps opening the pdf file in a new tab

I try to download a file directly from a link but Chrome keeps opening the pdf file in a new tab. 我尝试直接从链接下载文件,但Chrome一直在新标签页中打开pdf文件。 Here is the code I gathered from all issues I found : 这是我从发现的所有问题中收集的代码:

System.setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");

String downloadFilepath = "C:\\Users\\i016800\\Downloads";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
chromePrefs.put("download.directory_upgrade", "true");
chromePrefs.put("download.extensions_to_open", "");
chromePrefs.put("download.prompt_for_download", false);
chromePrefs.put("pdfjs.disabled", true);
chromePrefs.put("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
chromePrefs.put("plugins.plugins_disabled", new String[]{   // disable flash and the PDF viewer
    "Adobe Flash Player", "Chrome PDF Viewer"});

//Save Chrome Options
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> plugin = new HashMap<String, Object>();
plugin.put("enabled", true);
plugin.put("name", "Chrome PDF Viewer");
chromePrefs.put("plugins.plugins_list", Arrays.asList(plugin));
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--test-type");
options.addArguments("--always-authorize-plugins=true");
options.addArguments("--disable-extensions");
options.addArguments("start-maximized"); // Open Chrome in Full Screen

DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
cap.setPlatform(org.openqa.selenium.Platform.ANY);
cap.setCapability("prefs.download.directory_upgrade", true);

WebDriver driver = new ChromeDriver(options);

String adresseJarvis = "http://intranet.renault.com/declic-com/post/116235/2017/06/renault-assemblee-generale-2017/";
driver.get(adresseJarvis);  
driver.findElement(By.xpath("//html/body/div[1]/div[6]/div/div/div/div/div[2]/div[3]/div/div[1]/div/ul/li/a")).click();

I know some options are correctly loaded because when I activate options.addArguments("start-maximized"), Chrome begins in Full Screen. 我知道某些选项已正确加载,因为当我激活options.addArguments(“ start-maximized”)时,Chrome会以全屏模式启动。

I also tried to change chrome settings manualy before execution but it doesn't work neither. 我还尝试了在执行之前手动更改Chrome设置,但是这都不起作用。

My config : 我的配置:
Chrome Driver 2.29 Chrome驱动程序2.29
Java 1.7.0-60-b19 Java 1.7.0-60-b19
Eclipse Indigo build 20120216-1857 Eclipse Indigo版本20120216-1857
Os Windows 7 Entreprise 操作系统Windows 7企业
Chrome 58.0.3029.110 (64-bit) Chrome 58.0.3029.110(64位)
Selenium 2.47 硒2.47

A colleague of mine finnaly managed to solve my problem. 我的一个同事最终设法解决了我的问题。 Here is the code : 这是代码:

System.setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");
WebDriver driver = new ChromeDriver();
String adresseJarvis = "http://intranet.renault.com/declic-com/post/116235/2017/06/renault-assemblee-generale-2017/";
driver.get(adresseJarvis);
WebElement printLink=driver.findElements(By.xpath("//html/body/div[1]/div[6]/div/div/div/div/div[2]/div[3]/div/div[1]/div/ul/li/a")).get(0);
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])",printLink,"download","");
js.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])",printLink,"target","_blank");   
driver.findElement(By.xpath("//html/body/div[1]/div[6]/div/div/div/div/div[2]/div[3]/div/div[1]/div/ul/li/a")).click();

I hope it will help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM