Python / Selenium / Firefox: Can't start firefox with specified profile path

后端 未结 2 1704
清歌不尽
清歌不尽 2020-11-30 14:23

I try to start firefox with specified profile:

firefox_profile = webdriver.FirefoxProfile(\'/Users/p2mbot/projects/test/firefox_profile\')
driver = webdriver         


        
2条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 15:24

    I have spent around 2 hours (yes im so slow) guessing why didn't work. I've found why profile doesn't save back.

    Certainly, the profile passed to FirefoxProfile("myprofile/full/path") is used on the run, but, it's not saved back because (and maybe not obvious) selenium is for testing and testings should run with no cache, no profiles at all, clean as possible.

    Profile capability was (maybe) build to allow install certain extensions and settings before run test, but not for save them.

    The trick was to print out print driver.firefox_profile.path. It does differ from usual ones, with name tmp/tmpOEs2RR/webdriver-py-profilecopy instead instead of just tmp/tmpOEs2RR/, so reading that you should guess that a profile is being used.

    Now, the only remaining thing is to get it back :)

    Run this script, install something, edit something then run it again ;) :

    #!/usr/bin/env python
    #! -*- coding: utf-8 -*-
    
    import selenium
    from selenium import webdriver
    
    import os, sys, time
    
    # 1- set profile
    profile = os.path.dirname(sys.argv[0]) + "/selenita"
    fp = webdriver.FirefoxProfile(profile)
    driver = webdriver.Firefox(firefox_profile=fp)
    
    # 2- get tmp file location
    profiletmp = driver.firefox_profile.path
    
    # but... the current profile is a copy of the original profile :/
    print "running profile " + profiletmp
    
    driver.get("http://httpbin.org")
    time.sleep(2)
    raw_input("Press a key when finish doing things") # I've installed an extension
    
    # 3- then save back
    print "saving profile " + profiletmp + " to " + profile
    if os.system("cp -R " + profiletmp + "/* " + profile ):
        print "files should be copied :/"
    
    
    driver.quit()
    sys.exit(0)
    

    U can follow that schema or simple edit the FirefoxProfilea accordingly to your needs.

提交回复
热议问题