无法使用Selenium WebDriver将chrome(59 Latest)中的文件下载到特定目录,而无需任何窗口弹出窗口 [英] Not able to download file in chrome(59 Latest) to a specific directory without any window popup using Selenium WebDriver

查看:479
本文介绍了无法使用Selenium WebDriver将chrome(59 Latest)中的文件下载到特定目录,而无需任何窗口弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在chrome浏览器(59最新版本)下载一个文件到一个specfic目录,而不用弹出窗口。
使用下面的代码显示窗口弹出窗口。如果我不使用这个文件,它将被下载到下载文件夹而不显示任何窗口弹出。我看到很多人面临类似的问题,但是这些代码对他们来说效果不错。最新的Chrome有点问题吗?

I need to download a file in chrome browser(59 Latest Version) to a specfic directory without having window popup to show up. Using the below code it shows the window popup. If I don't use this the file will be downloaded to downloads folder without showing any windows popup. I have seen a lot of people faced the similar issue but this code worked well for them. Is it some issue with latest 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 以及一些简单的调整。代码块在我的最后工作正常。

As I tested this functionality with Selenium 3.4.0, ChromeDriver 2.30 & Chrome 59.0, I have tried with your own code to download a excel file from the url https://www.microsoft.com/en-in/download/details.aspx?id=45485 along with some simple tweaks. The code block works fine at my end.

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

让我知道这个答案是否是您的问题。

Let me know if this Answers your Question.

这篇关于无法使用Selenium WebDriver将chrome(59 Latest)中的文件下载到特定目录,而无需任何窗口弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆