gpt4 book ai didi

java - Chrome 不断在新选项卡中打开 pdf 文件

转载 作者:行者123 更新时间:2023-12-02 12:47:29 29 4
gpt4 key购买 nike

我尝试直接从链接下载文件,但 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 版本 20120216-1857
操作系统 Windows 7 Enterprise
Chrome 58.0.3029.110(64 位)
Selenium 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();

希望对您有所帮助。

关于java - Chrome 不断在新选项卡中打开 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44702786/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com