Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Chrome会在一个新选项卡中不断打开pdf文件_Java_Google Chrome_Selenium_Selenium Chromedriver - Fatal编程技术网

Java Chrome会在一个新选项卡中不断打开pdf文件

Java Chrome会在一个新选项卡中不断打开pdf文件,java,google-chrome,selenium,selenium-chromedriver,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<

我试图直接从一个链接下载一个文件,但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();
System.setProperty(“webdriver.chrome.driver”,“C:/Program Files(x86)/Google/chrome/Application/chromedriver.exe”);
String downloadFilepath=“C:\\Users\\i016800\\Downloads”;
HashMap chromePrefs=新HashMap();
chromePrefs.put(“profile.default\u content\u settings.popups”,0);
chromePrefs.put(“download.default\u目录”,downloadFilepath);
chromePrefs.put(“下载.目录\升级”,“真”);
chromePrefs.put(“下载.扩展到”打开“,”);
chromePrefs.put(“下载.提示下载”,false);
chromePrefs.put(“pdfjs.disabled”,true);
chromePrefs.put(“browser.helperApps.neverAsk.saveToDisk”,“application/pdf”);
chromePrefs.put(“plugins.plugins_disabled”),新字符串[]{//disableflash和PDF查看器
“Adobe Flash Player”、“Chrome PDF查看器”});
//保存Chrome选项
ChromeOptions选项=新的ChromeOptions();
HashMap plugin=新建HashMap();
plugin.put(“enabled”,true);
plugin.put(“名称”、“Chrome PDF查看器”);
chromePrefs.put(“plugins.plugins_list”,Arrays.asList(plugin));
HashMap chromeOptionsMap=新HashMap();
选项。设置实验选项(“prefs”,chromePrefs);
options.addArguments(“--test type”);
options.addArguments(“--始终授权插件=true”);
options.addArguments(“--disable extensions”);
options.addArguments(“开始最大化”);//全屏打开Chrome
DesiredCapabilities=DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY,ChromeOptions sMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
上限设置能力(色度选项、能力、选项);
setPlatform(org.openqa.selenium.Platform.ANY);
cap.setCapability(“prefs.download.directory\u upgrade”,true);
WebDriver=新的ChromeDriver(选项);
字符串adresseJarvis=”http://intranet.renault.com/declic-com/post/116235/2017/06/renault-assemblee-generale-2017/";
驱动程序。获取(adresseJarvis);
findElement(By.xpath(“//html/body/div[1]/div[6]/div/div/div/div[2]/div[3]/div/div/div[1]/div/ul/li/a”)。单击();
我知道一些选项被正确加载,因为当我激活options.addArguments(“开始最大化”)时,Chrome开始全屏显示

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

我的配置:
Chrome驱动程序2.29
Java 1.7.0-60-b19
Eclipse Indigo build 20120216-1857
操作系统Windows 7企业版
Chrome 58.0.3029.110(64位)

硒2.47

我的一位同事终于解决了我的问题。 代码如下:

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

我希望这会有所帮助。

我认为如果我们从该特定网站下载pdf,这可能是默认行为…@ylevoy我建议您遵循原则
KISS(保持简短)
。我们将仅配置必需的
chromePrefs
插件
ChromeOptions
所需功能
,以实现所需功能。你能不能缩小到你想要手动完成/查看的确切要求?谢谢你的回答。事实上,我尝试了许多选项和偏好的组合,一个接一个。我甚至一开始都没有这些东西;只有System.setProperty(“webdriver.chrome.driver”;我今天早上还尝试了其他首选项,比如-chromeeprefs.put(“plugins.plugins_disabled”,“adobeflash Player”);与“chrome PDF查看器”相同我真的不明白。我设置了Chrome,这样当我点击一个pdf链接时它就可以下载pdf,但是setpup是为Chrome.exe而不是chromedriver.exe完成的。所以当我手动打开Chrome时它可以工作,但它不工作。Selenium为我工作。这可能是最简单的解决方案。而且它避免了chromedriver参数复杂性的不断变化。另外呃,另一方面,操纵要测试的文档也不是我们经常想做的事情。