You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Selenium与GeckoDriver加载Firefox配置文件以实现登录识别

问题描述

我正在用Python、Selenium和GeckoDriver开发机器人,但每次登录网站都会触发新设备登录提示。为了让网站识别我的“设备”,我想加载已有的Firefox配置文件,但查了不少资料(很多都过时了)还是没搞明白。

现在的问题是用add_argument方法时,浏览器只会打开配置文件的文件夹路径,根本没加载配置文件。

以下是我尝试的代码:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options as options
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys

import random
import time
import getpass
import sys

class TestBot:
    def __init__(self, username, password):
        self.username = username
        self.password = password

        #Replace these lines
        #https://github.com/mozilla/geckodriver/releases --> Scroll to Assets and download appropriate file.   [For chrome, use ChromeDriver]
        new_driver_path = 'geckodriver-v0.33.0-win32/geckodriver.exe'
        #Location of your firefox browser.
        new_binary_path = r'C:\Users\[username]\AppData\Local\Mozilla Firefox\firefox.exe'

        ops = options()
        ops.add_argument(r"C:\Users\[username]\AppData\Roaming\Mozilla\Firefox\Profiles\a2bk0f2m.default-release")
        ops.binary_location = new_binary_path
        serv = Service(new_driver_path)
        self.bot = webdriver.Firefox(service=serv, options=ops)

#Deploy
username = "Test"
pwd = "test"
test = TestBot(username, pwd)
解决方案

你用add_argument加载Firefox配置文件的方式错误,这是Chrome的配置加载逻辑,Firefox需要通过专门的profile属性来指定配置文件。

修改后的代码如下:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options as options
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys

import random
import time
import getpass
import sys

class TestBot:
    def __init__(self, username, password):
        self.username = username
        self.password = password

        new_driver_path = 'geckodriver-v0.33.0-win32/geckodriver.exe'
        new_binary_path = r'C:\Users\[username]\AppData\Local\Mozilla Firefox\firefox.exe'
        # 配置文件路径
        profile_path = r'C:\Users\[username]\AppData\Roaming\Mozilla\Firefox\Profiles\a2bk0f2m.default-release'

        ops = options()
        # 关键:直接将配置文件路径赋值给profile属性
        ops.profile = profile_path
        ops.binary_location = new_binary_path
        serv = Service(new_driver_path)
        self.bot = webdriver.Firefox(service=serv, options=ops)

#Deploy
username = "Test"
pwd = "test"
test = TestBot(username, pwd)

重要提示

  • 替换代码中的[username]为你的系统实际用户名,确保配置文件路径正确且存在。
  • 运行代码前必须关闭所有正在运行的Firefox窗口,否则会提示配置文件被占用。
  • 加载配置文件后,Selenium启动的Firefox会复用该配置中的Cookie、浏览器标识等数据,网站会识别为你常用的设备,不再触发新设备登录提示。

内容的提问来源于stack exchange,提问作者Anthony

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 02:22:45