简体   繁体   中英

selenium doesn't set downloaddir in FirefoxProfile

i want to auto download files and save them in directory, everything is done but firefox stills save files in User download folder eg C:\\users\\root\\Downloads

the function in class PyWebBot

@staticmethod
def FirefoxProfile(path, handlers):
    from selenium import webdriver

    profile = webdriver.FirefoxProfile()
    profile.set_preference("browser.download.folderList",1)
    profile.set_preference("browser.download.manager.showWhenStarting",False)
    profile.set_preference("browser.download.dir", path)
    profile.set_preference("browser.download.downloadDir", path)
    profile.set_preference("browser.download.defaultFolder", path)
    profile.set_preference("browser.helperApps.alwaysAsk.force", False)
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk", handlers)
    profile.set_preference("pdfjs.disabled", True)
    profile.update_preferences()

    return profile

then

 def setUp(self):
        self.profile = PyWebBot.FirefoxProfile(config['downloads'], config['handlers'])
        self.driver = webdriver.Firefox(self.profile)
    ...
    ...

config:

config['downloads'] = 'Q:/web2py_src/web2py/applications/internet2letter/private/testing/selenium/downloads'
config['handlers'] = 'application/pdf'

There are couple methods to a solution for this problem,

  1. Make sure that the path is valid. Use something like, os.path.exists or os.isfile
  2. When the Firefox launches with the selenium driver, navigate to about:config and check the look up browser.download.dir , to make sure there was a change.
  3. Finally, make sure that profile.set_preference ( profile.set_preference("browser.download.folderList",2 ) has 2 as a second argument, since 0 means to download to the desktop, 1 means to download to the default "Downloads" directory, 2 means to use the directory you specify in "browser.download.dir"
  4. Make sure your path is noted with back slashes '\\' not forward slashes '/'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM