且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-03-12 22:42:35

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

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. 在 Windows 框中找到 Firefox Profile 目录.例如我的 Firefox 配置文件 "debanjan" 位于 C:\Users\AtechM_03\AppData\Roaming\Mozilla\Firefox\Profiles,名称为 w8iy627a.debanjan.
  2. 接下来,您必须在启动webdriver 时指定Firefox Profile 目录的绝对路径.
  3. 这是在我的 Windows 机器上打开现有 Firefox Profile 'debanjan' 的工作代码:

  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:

需要注意的是,当前的 Selenium-Python 绑定对于 geckodriver 是不稳定的,并且看起来是特定于架构的.您可以找到 github 讨论在这里合并.因此,您可能还需要在初始化 webdriver

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)