【问题标题】:Selenium java chrome driver, get last download nameSelenium java chrome 驱动,获取最后下载的名称
【发布时间】:2017-03-16 11:43:48
【问题描述】:

问题是这样的:我在多个情况下在线程中运行 selenium 测试。在测试期间,单击“下载”按钮。此按钮调用生成 PDF 并提供下载的 Ajax。它默认下载到“下载”。

我需要将这些下载移动到特定位置(每个测试都有一个),我不知道文件的名称。

我曾尝试在测试期间更改下载目录,但似乎不可能。

我尝试打开“下载”选项卡 (chrome://downloads/) 并浏览它,但似乎不可能,它没有找到网络元素

我已尝试移动(从原点复制和删除)las 文件,但由于在线程中运行 severan 测试,这可能是个问题。

有什么想法吗?

提前致谢

【问题讨论】:

  • 您好!感谢您的链接,但它不起作用。
  • 我最后做的是为每个测试配置不同的下载路径(String rutaDescarga ="C:\\Users\\XXX\\Downloads"+System.currentTimeMillis() +Math.random();) whit这个,每个测试用例都有自己的路径,复制文件时没有错误

标签: java selenium selenium-chromedriver


【解决方案1】:

我最后做的是为每个测试配置不同的下载路径

String rutaDescarga ="C:\\Users\\XXX\\Downloads"+System.currentTimeMillis() +Math.random();
File creaRuta = new File(rutaDescarga);
if(!creaRuta.exists()){
    creaRuta.mkdirs();
}
downloadPath = rutaDescarga;    
chromePref.put("download.default_directory",rutaDescarga);
options.setExperimentalOptions("prefs", chromePref);` 

这样,每个测试用例都有自己的路径,复制文件时不会出错

【讨论】:

    【解决方案2】:

    您可以指定下载位置。下面的代码可能会帮助您尝试一下

    文件 file = new File("./downloads");

            boolean b = false;
    
            if (!file.exists()) {
    
              b = file.mkdirs();
            }
    
            FileUtils.cleanDirectory(file);
            String downloadFolder = System.getProperty("user.dir")+"/downloads";
    
        if (browser.equalsIgnoreCase("Chrome")) {
    
            HashMap<String, Object> chromePref = new HashMap<>();
            chromePref.put("download.default_directory", downloadFolder);
            chromePref.put("download.prompt_for_download", "false");
            ChromeOptions options = new ChromeOptions();
            options.setExperimentalOption("prefs", chromePref);
            driver = new ChromeDriver(options);
    
        } else if (browser.equalsIgnoreCase("Firefox")) {
    
            FirefoxProfile profile = new FirefoxProfile();
            profile.setPreference("browser.download.dir",downloadFolder );  // folder
            profile.setPreference("pdfjs.disabled", true);  // disable the built-in viewer
            profile.setPreference("browser.download.folderList", 2);
            profile.setPreference("browser.download.panel.shown", false);
            profile.setPreference("browser.helperApps.neverAsksaveToDisk", "application/x-msexcel,application/excel,application/x-excel,application/excel,application/x-excel,application/excel,application/vnd.ms-excel,application/x-excel,application/x-msexcel");
    
            FirefoxOptions firefoxOptions = new FirefoxOptions();
            firefoxOptions.setCapability(FirefoxDriver.PROFILE, profile);
            firefoxOptions.setCapability(FirefoxDriver.MARIONETTE, true);
            firefoxOptions.setCapability(CapabilityType.ELEMENT_SCROLL_BEHAVIOR, 0);
    
            driver = new FirefoxDriver(firefoxOptions);
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-22
      • 1970-01-01
      相关资源
      最近更新 更多