2017-06-22 171 views
0

PDF文件我尝试直接从链接下载文件,但是Chrome会在新标签中打开PDF文件。 这是我从我发现的所有问题聚集代码:Chrome会打开一个新标签

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(); 

我知道有些选项是正确加载,因为当我启动options.addArguments(“启动最大化”),Chrome的全屏开始。

我也试过在执行前manualy更改Chrome设置,但它不工作也没有。

我的配置:
铬驱动2.29
的Java 1.7.0-60-B19
Eclipse的靛蓝建立20120216-1857
操作系统Windows 7企业公司
的Chrome 58.0.3029.110(64位)
硒2.47

+0

我认为这可能是默认的行为,如果我们从该特定网站... –

+0

下载PDF @ylevoy我会建议你遵循一个原则'KISS(保持短期和简单)'。我们将只配置强制'chromePrefs','plugin','ChromeOptions'和'DesiredCapabilities'达到什么是必需的。你可以缩小到你确切的要求,因为你想手动/手动查看它吗?谢谢 – DebanjanB

+0

感谢您的回答。事实上,我尝试了很多很多的选项和偏好组合,一个接一个地尝试。我甚至开始没有任何这些;只有System.setProperty( “webdriver.chrome.driver”;我也试过早上像 其它偏好 - chromePrefs.put( “plugins.plugins_disabled”, “的Adobe Flash Player”); - 同王氏 “Chrome PDF查看器” 我真的不明白 我设置Chrome使其下载PDF时,当我点击一个pdf链接,但setpup是为chrome.exe而不是为chromedriver.exe完成的,所以它工作时我手动打开Chrome,但它doesn'硒工作。 – ylevoy

回答

0

我的一位同事finnaly设法解决我的问题。 这里是代码:

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(); 

我希望这会有所帮助。

相关问题