How to download files using GeckoDriver Firefox and Selenium?

前端 未结 1 1323
南笙
南笙 2020-12-19 15:46

I used this code for download file , but its not working

FirefoxProfile profile = new FirefoxProfile();

profile.setPreference(\"browser.download.dir\",\"D:\         


        
相关标签:
1条回答
  • 2020-12-19 16:08

    To download the file clicking on the link with text as Test File to Download you need to:

    • Create a new FirefoxProfile() and set the required preferences.
    • Use an instance of FirefoxOptions() set the profile.
    • You can use the following solution:

      System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
      FirefoxProfile profile = new FirefoxProfile();
      profile.setPreference("browser.download.dir", "C:\\Utility\\Downloads");
      profile.setPreference("browser.download.folderList",2);
      profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
      profile.setPreference("browser.download.manager.showWhenStarting", false);
      profile.setPreference("browser.helperApps.neverAsk.openFile","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
      profile.setPreference("browser.helperApps.alwaysAsk.force", false);
      profile.setPreference("browser.download.manager.useWindow", false);
      profile.setPreference("browser.download.manager.focusWhenStarting", false);
      profile.setPreference("browser.download.manager.showAlertOnComplete", false);
      profile.setPreference("browser.download.manager.closeWhenDone", true);
      FirefoxOptions options = new FirefoxOptions();
      options.setProfile(profile);
      WebDriver driver = new FirefoxDriver(options);
      driver.get("http://toolsqa.com/automation-practice-form/");
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Test File to Download"))).click();
      
    0 讨论(0)
提交回复
热议问题