4

I am trying to download a file using the Selenium Chrome web driver but I don't know how to deal with save as dialog box.

I have seen many answers on how to do this using Firefox but none using Chrome.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Ashish Verma
  • 245
  • 1
  • 3
  • 13

1 Answers1

5

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2) # custom location profile.set_preference('browser.download.manager.showWhenStarting', False) profile.set_preference('browser.download.dir', '/tmp') profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv')

After setting these preferences the browser won't show up pop dialog asking for whether you want to download save or other. When can then just use find_some_eleme = driver.find_element_by_xpath('''<somexpath>''').click() we can use any other method of locating the element xpath/id/css/name... and we use the method click() freely because there won't be a dialog. or .setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream,text/csv")

For Chrome:

chromedriver = "path/to/chromedriver"

os.environ["webdriver.chrome.driver"] = chromedriver
chrome_options = Options()

# this is the preference we're passing
prefs = {'profile.default_content_setting_values.automatic_downloads': 1}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
innicoder
  • 2,612
  • 3
  • 14
  • 29