【问题标题】:Mozilla's profiles with SeleniumMozilla 的 Selenium 配置文件
【发布时间】:2016-12-12 16:30:42
【问题描述】:

我正在尝试为 Mozilla Firefor 创建配置文件,以便在没有对话框的情况下下载 csv 文件:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/vnd.ms-excel'))

binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe')

driver = webdriver.Firefox(firefox_profile=profile,firefox_binary=binary)

当我到达下载文件时,它会打开对话框,我的配置文件是“默认”的。我做错了什么?

【问题讨论】:

    标签: python python-2.7 selenium mozilla


    【解决方案1】:

    如果加了会怎样

    application/csv,text/csv
    

    ,即:

    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    
    profile = webdriver.FirefoxProfile()
    profile.set_preference('browser.download.panel.shown', false)
    profile.set_preference('browser.download.folderList', 2)
    profile.set_preference('browser.download.manager.showWhenStarting', False)
    profile.set_preference('browser.download.dir', os.getcwd())
    profile.set_preference('browser.helperApps.neverAsk.openFile', ('application/csv,text/csv,application/vnd.ms-excel,application/x-msexcel,application/excel,application/x-excel,text/comma-separated-values')) 
    profile.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/csv,text/csv,application/vnd.ms-excel,application/x-msexcel,application/excel,application/x-excel,text/comma-separated-values'))
    profile.set_preference('browser.download.manager.alertOnEXEOpen', false)
    profile.set_preference('browser.download.manager.focusWhenStarting', false)
    profile.set_preference('browser.download.manager.useWindow', false)                                                    
    profile.set_preference('browser.download.manager.showAlertOnComplete', false)
    profile.set_preference('browser.download.manager.closeWhenDone', true)
    binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe')
    
    driver = webdriver.Firefox(firefox_profile=profile,firefox_binary=binary)
    

    【讨论】:

    • 同样的事情,没有任何变化
    • 它是否适用于您提到的那些文件的新编辑?
    • 我做了一些修改。您是在尝试其他文件类型还是只是 csv 文件?
    • 它也不起作用。文件类型为:“Microsoft Excel 逗号分隔值文件”
    • 但是,如果我尝试使用 XLS 文件,如果我使用这个“application/vnd.ms-excel”,它就可以工作
    猜你喜欢
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 2020-06-14
    • 2019-12-13
    相关资源
    最近更新 更多