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)

 

建議使用第二種,這樣不須要設置瀏覽器,可移植性好。

相關文章
相關標籤/搜索