我需要在chrome浏览器(最新版本为59)中下载文件到特定目录,而无需弹出窗口。使用以下代码显示窗口弹出窗口。如果我不使用此文件,则文件将下载到downloads文件夹,而不会显示任何窗口弹出窗口。我已经看到很多人面临类似的问题,但是这段代码对他们来说效果很好。最新的Chrome浏览器有问题吗?
String downloadFilepath = TestConstants.FILE_PATH; HashMap<String, Object> chromePrefs = new HashMap<String, Object>(); chromePrefs.put("profile.default_content_settings.popups", 0); chromePrefs.put("download.prompt_for_download", "false"); chromePrefs.put("download.default_directory", downloadFilepath); ChromeOptions options = new ChromeOptions(); options.setExperimentalOption("prefs", chromePrefs); DesiredCapabilities cap = DesiredCapabilities.chrome(); cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); cap.setCapability(ChromeOptions.CAPABILITY, options); driver = new ChromeDriver(cap);
当我使用Selenium 3.4.0,ChromeDriver 2.30和Chrome 59.0测试此功能时,我尝试使用自己的代码从URL下载excel文件https://www.microsoft.com/en- in/download/details.aspx?id=45485以及一些简单的调整。该代码块在我的末端工作正常。
https://www.microsoft.com/en- in/download/details.aspx?id=45485
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe"); String downloadFilepath = "C:\\Utility\\OP_Resources\\ChromeDownload"; HashMap<String, Object> chromePrefs = new HashMap<String, Object>(); chromePrefs.put("profile.default_content_settings.popups", 0); chromePrefs.put("download.prompt_for_download", "false"); chromePrefs.put("download.default_directory", downloadFilepath); ChromeOptions options = new ChromeOptions(); options.setExperimentalOption("prefs", chromePrefs); options.addArguments("start-maximized"); options.addArguments("disable-infobars"); DesiredCapabilities cap = DesiredCapabilities.chrome(); cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); cap.setCapability(ChromeOptions.CAPABILITY, options); WebDriver driver = new ChromeDriver(cap); driver.get("https://www.microsoft.com/en-in/download/details.aspx?id=45485"); JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("window.scrollBy(0,200)", ""); driver.findElement(By.linkText("Download")).click();