硒未在FirefoxProfile中设置downloaddir [英] selenium doesn't set downloaddir in FirefoxProfile

查看:90
本文介绍了硒未在FirefoxProfile中设置downloaddir的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动下载文件并将其保存在目录中,一切都已完成,但是Firefox仍将文件保存在用户下载文件夹中,例如C:\users\root\Downloads

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

PyWebBot类中的函数

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

然后

 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. 确保路径有效.使用类似os.path.existsos.isfile
  2. 的名称
  3. 使用硒驱动程序启动Firefox时,导航至about:config并检查browser.download.dir,以确保进行了更改.
  4. 最后,确保profile.set_preference(profile.set_preference("browser.download.folderList",2)具有2作为第二个参数,因为0表示要下载到桌面,1表示要下载到默认的下载"目录, 2表示使用您在"browser.download.dir"中指定的目录
  5. 确保您的路径标有反斜杠"\"而不是正斜杠 斜杠"/"
  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 '/'

这篇关于硒未在FirefoxProfile中设置downloaddir的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆