Chrome会在新标签

时间:2017-06-22 14:49:52

标签: java google-chrome selenium selenium-chromedriver

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

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(“start-maximized”)时,Chrome会以全屏模式开始。

我还尝试在执行前手动更改chrome设置,但它也不起作用。

我的配置:
Chrome驱动程序2.29
Java 1.7.0-60-b19
Eclipse Indigo build 20120216-1857
Os Windows 7 Entreprise
Chrome 58.0.3029.110(64位)
硒2.47

1 个答案:

答案 0 :(得分:1)

我的一位同事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();

我希望它会有所帮助。

相关问题