Download MP4 file instead of playing it using ChromeDriver?

后端 未结 3 1895
半阙折子戏
半阙折子戏 2020-12-16 02:33

I\'m using Chrome Web Driver 2.10 chromedriver_win32.zip with Selenium WebDriver 2.31.2.

With verbose logging enabled it seems the DesiredCapabilities

相关标签:
3条回答
  • 2020-12-16 03:06

    I got this working by below code:

    System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
    String downloadFilepath = "/path/to/download";
    HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
    chromePrefs.put("profile.default_content_settings.popups", 0);
    chromePrefs.put("download.default_directory", downloadFilepath);
    ChromeOptions options = new ChromeOptions();
    HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
    options.setExperimentalOptions("prefs", chromePrefs);
    options.addArguments("--test-type");
    DesiredCapabilities cap = DesiredCapabilities.chrome();
    cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
    cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    cap.setCapability(ChromeOptions.CAPABILITY, options);
    WebDriver driver = new ChromeDriver(cap);
    
    0 讨论(0)
  • 2020-12-16 03:13

    This worked greatly for me:

    DesiredCapabilities cap = DesiredCapabilities.firefox();
    
    FirefoxProfile fprofile= new FirefoxProfile();
    fprofile.setPreference("browser.download.folderList",2);  //0-desktop,1-file download folder,2-specified location
    fprofile.setPreference("browser.download.manager.showWhenStarting", false); //prevent download file window
    fprofile.setPreference("browser.download.dir","E:\\Downloadfilebyprogram");
    fprofile.setPreference("browser.download.manager.focusWhenStarting", false);
    //fprofile.setPreference("browser.helperApps.alwaysAsk.force", false);
    //fprofile.setPreference("browser.download.manager.closeWhenDone", true);
    //fprofile.setPreference("browser.download.manager.useWindow", false);
    //fprofile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
    //fprofile.setPreference("browser.helperApps.neverAsk.openFile,","application/vnd.ms-excel");
    fprofile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/vnd.ms-excel");
    fprofile.setPreference("browser.download.manager.alertOnExeOpen", false); //prevent from opening a file
    
    cap.setCapability(FirefoxDriver.PROFILE, fprofile);
    WebDriver driver=new FirefoxDriver(cap);
    
    0 讨论(0)
  • 2020-12-16 03:27
    ChromeOptions options = new ChromeOptions();
    Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("profile.default_content_settings.popups", 0);
    prefs.put("download.default_directory", getClass().getResource("/data/input").toString().replace("%20", " ").replace("file:","").replaceFirst("/", ""));
    options.setExperimentalOption("prefs", prefs);
    
    options.addArguments("--test-type");
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    
    0 讨论(0)
提交回复
热议问题