2017-06-22 170 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(); 

我希望這會有所幫助。

相關問題