使用 Selenium 和 GeckoDriver 加载 Firefox 配置文件并识别登录

问题描述 投票:0回答:1

所以我正在使用 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)
python selenium-webdriver firefox geckodriver
1个回答
0
投票

我发现我需要添加以下行:

ops.add_argument("-profile")

我不得不运行该程序几次,但它确实记得我之后的登录。

© www.soinside.com 2019 - 2024. All rights reserved.