Search code examples
google-chromeseleniumwebdriverselenium-chromedriver

How to download a file with Selenium and ChromeDriver


I have a website to test with selenium and ChromeDriver (on windows), where I like to test the functionality to export data and import it again.

The export creates a xml file that is downloaded on ones computer. When running this with webdriver, Chrome asks me whether to keep the file or discard it, as it might be a potential threat.

How can I switch off this behavior inside my test ? Is there a chrome setting I can use, so that a file is no matter what downloaded ?

Thanks


Solution

  • Try this. Executed on windows

    (How to control the download of files with Selenium Python bindings in Chrome)

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_experimental_option("prefs", {
      "download.default_directory": r"C:\Users\xxx\downloads\Test",
      "download.prompt_for_download": False,
      "download.directory_upgrade": True,
      "safebrowsing.enabled": True
    })