Selenium - 如何从现有的 Firefox 配置文件导入所有设置

Ric*_*son 5 python firefox selenium python-3.x

我为什么要这样做:

我需要自动化一个需要客户端 SSL 证书的网站。我理解这是一个无法使用 fp.set_preference() 指定的选项。我无法控制要连接的服务器,因此无法更改安全设置。

我试过什么

我创建了一个单独的 Firefox 配置文件,其中设置了所需的“客户端密码保护 SSL 证书”,自动选择一个证书和一些手动代理设置 (SOCKS 5)。经过多次谷歌搜索后,我将代码设置如下:

from selenium import webdriver
url = 'https://www.paininneck.co.uk'
fp = webdriver.FirefoxProfile(r"""C:\Users\
<user>\AppData\Local\Mozilla\Firefox\Profiles\<Firefox>""")
driver = webdriver.Firefox(fp)
driver.get(url)
Run Code Online (Sandbox Code Playgroud)

问题:

浏览器确实打开了,但是,它仍然使用默认配置文件。我在其他配置文件中更改的任何设置都没有复制。我的代码中指定的配置文件仍在通过 Firefox UI 选择它。

我希望我错过了一些简单的东西,而且所有这些时间谷歌搜索都没有白费!我不愿意更改为默认设置,但是在调整默认配置文件以查看设置是否会复制后,很明显它们不会,并且 Selenium 每次都在制作干净的副本。

亲切的问候

富有的

版本:

Python==3.6.1,
Selenium==3.4.3,
Firefox==53
gecko driver==v0.16.1
OS==Windows(Its for work dont judge me!)
Run Code Online (Sandbox Code Playgroud)

Deb*_*anB 2

使用 Selenium 3.4.x、Python 3.6.1 以及 geckodriver v0.16.1 和 Mozilla Firefox 53.0,您可以通过以下步骤使用现有的 Firefox 配置文件:

  1. 在 Windows 上找到 Firefox 配置文件目录。例如,我的 Firefox 配置文件的名称"debanjan"为。C:\\Users\\AtechM_03\\AppData\\Roaming\\Mozilla\\Firefox\\Profilesw8iy627a.debanjan
  2. 接下来,您必须在启动 .conf 文件时指定 Firefox 配置文件目录的绝对路径webdriver
  3. 以下是在我的 Windows 计算机上打开现有 Firefox 配置文件的工作代码'debanjan'

    值得注意的是,当前的 Selenium-Python 绑定对于 geckodriver 来说不稳定,并且看起来是特定于架构的。你可以在这里找到 github讨论合并因此,在初始化webdriver时,您可能还需要传递 firefox 二进制文件的绝对路径

    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)
    
    Run Code Online (Sandbox Code Playgroud)