【问题标题】:Downloading files in folder using selenium webdriver使用 selenium webdriver 下载文件夹中的文件
【发布时间】:2017-08-03 11:27:16
【问题描述】:

我正在使用 selenium 下载文件夹中的文件。我可以看到我在该文件夹中下载的所有文件,但扩展名为 .docx 的文件未下载到该文件夹​​。这些文件正在我系统的下载文件夹中下载。

我正在使用的代码片段。

    if (driverpath.endsWith(".exe")) {
        System.setProperty("webdriver.chrome.driver", driverpath);
    } else {
        System.setProperty("webdriver.chrome.driver", driverpath + "chromedriver.exe");
    }
    ChromeOptions options = new ChromeOptions();
    new File(System.getProperty("user.home") + File.separator + "Desktop" + File.separator
            + "Redesign_Downloaded_Files").mkdir();
    HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
    chromePrefs.put("profile.default_content_settings.popups", 0);
    String downloadFilepath = System.getProperty("user.home") + File.separator + "Desktop" + File.separator
            + "Redesign_Downloaded_Files";
    chromePrefs.put("download.default_directory", downloadFilepath);
    options.setExperimentalOption("prefs", chromePrefs);
    options.addExtensions(new File(System.getProperty("user.dir") + File.separator + "extension_1_5_294.crx"));
    options.addArguments("--disable-popup-blocking");
    DesiredCapabilities cap = DesiredCapabilities.chrome();
    cap.setCapability(ChromeOptions.CAPABILITY, options);
    return new ChromeDriver(cap);

有没有其他方法可以设置扩展文件应该下载到该文件夹​​?

【问题讨论】:

  • 能否尝试更新chromedriver、浏览器和单机并检查

标签: java selenium automation


【解决方案1】:

对于你的情况,欢迎你试试我最近开源的library: 它将允许您在一个代码行中全局和本地定义您的下载文件夹,以及其他与下载相关的功能。

使用库在测试中轻松设置下载文件夹:

private SeleniumDownloadKPI seleniumDownloadKPI;

@BeforeEach
void setUpTest() {

    // New instance of SeleniumDownloadKPI with given download folder.
    seleniumDownloadKPI =
         new SeleniumDownloadKPI("/tmp/downloads");
    ChromeOptions chromeOptions =
            seleniumDownloadKPI.generateDownloadFolderCapability();
    driver = new ChromeDriver(chromeOptions);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 2017-08-26
    • 2018-04-14
    • 1970-01-01
    • 2019-12-28
    相关资源
    最近更新 更多