Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

python - Selenium - How to import all settings from an existing Firefox profile

Why am I doing this:

I need to automate a website that requires client-side SSL certificates. I understand this to be an option which cannot be specified using fp.set_preference(). I am not in control of the server I am connecting to thus I cannot change the security setup.

What have I tried

I have created a separate Firefox profile which has the required 'client-side password protected SSL certificates' set up, select one certificate automaticaly and some manual proxy settings (SOCKS 5). After much googling I have set my code as follows:

from selenium import webdriver
url = 'https://www.paininneck.co.uk'
fp = webdriver.FirefoxProfile(r"""C:Users
<user>AppDataLocalMozillaFirefoxProfiles<Firefox>""")
driver = webdriver.Firefox(fp)
driver.get(url)

The Problem:

The browser does open, however, it is still using the default profile. None of the settings I have changed in the other profile has copied across. The profile specified in my code is still working with selecting it through the Firefox UI.

I am hoping I missed something simple and all this time googling has not been in vain! I am reluctant to change to default settings, however after tweaking the default profile to see if the settings would copy over it is apparent that they don't and Selenium is making a clean copy each time.

Kind regards

Rich

Versions:

Python==3.6.1,
Selenium==3.4.3,
Firefox==53
gecko driver==v0.16.1
OS==Windows(Its for work dont judge me!)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using Selenium 3.4.x, Python 3.6.1 along with geckodriver v0.16.1 & Mozilla Firefox 53.0, you can use the existing Firefox profile through the following steps:

  1. Locate the Firefox Profile directory on your windows box. For e.g. my Firefox Profile "debanjan" was located at C:\Users\AtechM_03\AppData\Roaming\Mozilla\Firefox\Profiles by the name w8iy627a.debanjan.
  2. Next, you have to specify the absolute path of the Firefox Profile directory when you initiate the webdriver.
  3. Here is the working code which opens an existing Firefox Profile 'debanjan' on my Windows machine:

    It is to be noted that the current Selenium-Python binding is unstable with geckodriver and looks to be Architecture specific. You can find the github discussion and merge here. So you may additionally need to pass the absolute path of the firefox binary while initializing the webdriver

    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
    
    profile = webdriver.FirefoxProfile('C:\Users\AtechM_03\AppData\Roaming\Mozilla\Firefox\Profiles\w8iy627a.debanjan')
    binary = FirefoxBinary('C:\Program Files\Mozilla Firefox\firefox.exe')
    
    driver = webdriver.Firefox(firefox_profile=profile, firefox_binary=binary, executable_path="C:\Utility\BrowserDrivers\geckodriver.exe")
    url = 'https://www.paininneck.co.uk'
    driver.get(url)
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...