Setting selenium to use custom profile, but it keeps opening with default

后端 未结 3 1987
悲哀的现实
悲哀的现实 2020-12-01 06:20

I am trying to use python and selenium to automate some tasks in firefox. When I download a file, that pop up comes up asking if you want to open or save, and a check box fo

相关标签:
3条回答
  • 2020-12-01 06:51

    Error: fp.set_preference("browser.download.dir",getcwd()) NameError: name 'getcwd' is not defined

    getcwd() is not defined. So I assume you want the getcwd from the os module:

    • http://docs.python.org/library/os.html

    add: import os , and then invoke with os.getcwd().

    or you could just add the import for this function: from os import getcwd

    your example with the proper imports included:

    import os
    from selenium import webdriver
    
    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', 'text/csv/xls')
    driver = webdriver.Firefox(profile)
    
    0 讨论(0)
  • 2020-12-01 07:01

    I did the following:

    Open Profile Directory

    Or:

    Linux: ls -d /home/$USER/.mozilla/firefox/*.default/ to see user profile directories

    Mac: ls -d ~/Library/Application\ Support/Firefox/Profiles/*

    Output:

    /home/jmunsch/.mozilla/firefox/xfoyzfsb.default/
    /home/jmunsch/.mozilla/firefox/yxjwk1py.default/
    

    To load a custom user profile I ran through creating a profile in firefox and then did the following with the python selenium webdriver code:

    def setUp(self):
        self.profile = webdriver.FirefoxProfile('/home/jmunsch/.mozilla/firefox/yxjwk1py.default')
        self.driver = webdriver.Firefox(self.profile)
    

    System Info:

    Python 2.7.3 (default, Sep 26 2013, 20:08:41) 
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pkg_resources;pkg_resources.get_distribution("selenium").version
    
    jmunsch@NE-522:~/Desktop/work$ firefox --version
    Mozilla Firefox 26.0
    

    also note

    @Corey's answer to manually set a profile

    All of the configurables can be found under about:config:

    profile.set_preference('browser.download.folderList', 2)

    0 讨论(0)
  • 2020-12-01 07:12

    You should add this:

    profile.set_preference("browser.helperApps.neverAsk.openFile",
        "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml")
    

    It does work!

    0 讨论(0)
提交回复
热议问题