Python WebDriver + Firefox 文件下载

 

 

firefox能够经过 在地址栏输入:about:config 或about:aupport 来查看或修改配置信息。 web

 

这里有两种解决方式,浏览器

一、设置自动保存下载app

以下图勾选:之后自动采用相同的动做处理此类文件spa

这样下次在下载该类型的文件时就不会这样提醒了。firefox

 

若是想修改设置能够在 浏览器选项中进行修改code

以下图orm

 

 

 这样设置完成后,可是程序启动时打开的浏览器并无按照这种配置打开。xml

 对比下图能够发现,经过webdriver打开的浏览器与手工打开的浏览器展现的不一样,这是由于webdriver打开的浏览器没有按照浏览器设置的配置文件打开。若是想按照配置文件打开,在打开以前要先获取配置文件信息。blog

 

加入代码以下: ip

        profile =  webdriver.FirefoxProfile(r"C:\Users\Skyyj\AppData\Roaming\Mozilla\Firefox\Profiles\1rzh6139.default")

        self.driver = webdriver.Firefox(profile)

 

 

 

 二、第二种方法

就是在代码中加入配置信息

 

 

经过about:config

经过%APPDATA%\Mozilla\Firefox\Profiles\找到默认配置  

找到mimeTypes.rdf目录,用其它方式打开,查找你刚刚保存的文件类型

mimeTypes.rdf 就存在上面 profile 的配置路径中

C:\Users\Skyyj\AppData\Roaming\Mozilla\Firefox\Profiles\1rzh6139.default

查找fileExtensions="xlsx"

NC:value="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
NC:editable="true"
NC:fileExtensions="xlsx"
NC:description="xlsx File">

从文件中就能够得知咱们须要的文件类型是什么

 

 代码以下:

        #profile =  webdriver.FirefoxProfile(r"C:\Users\Skyyj\AppData\Roaming\Mozilla\Firefox\Profiles\1rzh6139.default")
        profile = webdriver.FirefoxProfile()

        ##设置成0表明下载到浏览器默认下载路径;设置成2则能够保存到指定目录
        profile.set_preference("browser.download.folderList", 2)
        #这里设置与否不影响,没有发现有什么影响。
        #profile.set_preference("browser.download.manager.showWhenStarting", False)
        profile.set_preference("browser.download.dir", r"c:\Down")
        profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
        #这里设置与否没有发现有什么影响
        #profile.set_preference("browser.helperApps.alwaysAsk.force", False);
        self.driver = webdriver.Firefox(profile)

 

建议使用第二种,这样不须要设置浏览器,可移植性好。

相关文章
相关标签/搜索