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

Selenium ChromeDriver初始化报错:修复options参数多值问题

解决Selenium中WebDriver.__init__() got multiple values for argument 'options'错误

我正在开发一款浏览器自动化程序,将代码拆分为main.py(包含BrowserAutomation类及相关方法)和automate.py(调用该类),期望自动打开Chrome标签页并加载指定URL。运行automate.py时出现如下错误:

TypeError: WebDriver.init() got multiple values for argument 'options'


现有代码

main.py

import sys
import logging
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
LOGGER = logging.getLogger()

class BrowserAutomation(object):
    def __init__(self, browser=None, time_out=1000):
        ""A method to deal with browser opening""
        self.BASE_URL ="https://erita.rita.go.tz/auth"

        if not browser:
            browser = webdriver.Chrome(
                ChromeDriverManager().install(),
                options=self.chrome_options,
            )

            handles = browser.window_handles
            for _, handle in enumerate(handles):
                if handle != browser.current_window_handle:
                    browser.switch_to.window(handle)
                    browser.close()
        
        self.browser = browser
        self.wait = WebDriverWait(self.browser, time_out)
        self.cli()
        self.login()

    @property
    def chrome_options(self):
        chrome_options = Options()
        if sys.platform == "win32":
            chrome_options.add_argument("--profile-directory=Default")
            chrome_options.add_argument("--user-data-dir=C:/Temp/ChromeProfile")
        else:
            chrome_options.add_argument("start-maximized")
            chrome_options.add_argument("--user-data-dir=./User_Data")

        return chrome_options
    
    def cli(self):
        """
        LOGGER settings  [nCKbr]
        """
        handler = logging.StreamHandler()
        handler.setFormatter(
            logging.Formatter(
                "%(asctime)s - %(name)s -- [%(levelname)s] >> %(message)s"
            )
        )
        LOGGER.addHandler(handler)
        LOGGER.setLevel(logging.INFO)    
    

    def open_window(self):
        self.browser.get(self.BASE_URL)
        time.sleep(2)
        self.browser.maximize_window()

    def login(self):
        xpath = "/html/body/header/nav/div/div/div/button[2]"
        email = "/html/body/div[1]/main/div[2]/div[2]/div/div[2]/div/div/form/div[1]/div[2]/input"
        password = "/html/body/div[1]/main/div[2]/div[2]/div/div[2]/div/div/form/div[2]/div[2]/input"
        login = "/html/body/div[1]/main/div[2]/div[2]/div/div[2]/div/div/form/div[3]/div/button"
        signin_btn = self.wait.until(
            EC.presence_of_element_located(
                (
                    By.XPATH,
                    xpath,
                )
            )
        )
        signin_btn.click()

        email_field = self.wait.until(
            EC.presence_of_element_located(
                (
                    By.XPATH,
                    email,
                )
            )
        )
        time.sleep(2)
        email_field.send_keys("xxxx@gmail.com")

        password_field = self.wait.until(
            EC.presence_of_element_located(
                (
                    By.XPATH,
                    password
                )
            )
        )
        password_field.send_keys("xxxx")

        login_btn = self.wait.until(
            EC.presence_of_element_located(
                (
                    By.XPATH,
                    login
                )
            )
        )
        login_btn.click()

automate.py

from main import BrowserAutomation

automate = BrowserAutomation()

错误信息

(venv) PS C:\Users\Administrator\Desktop\e-rita\E-rita> python automate.py
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\e-rita\E-rita\automate.py", line 3, in <module>
    automate = BrowserAutomation()
               ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Administrator\Desktop\e-rita\E-rita\main.py", line 19, in __init__   
    browser = webdriver.Chrome(
              ^^^^^^^^^^^^^^^^^
TypeError: WebDriver.__init__() got multiple values for argument 'options'

解决方案

核心修改

错误根源是Selenium 4.x版本调整了webdriver.Chrome()的参数结构:第一个参数不再是驱动路径,而是options。直接传入ChromeDriverManager().install()会被误识别为options参数,后续又显式传递options=self.chrome_options,导致参数冲突。

需要新增Service类导入,并将驱动路径通过service参数传入:

  1. 在main.py顶部新增导入:
from selenium.webdriver.chrome.service import Service
  1. 修改__init__方法中的Chrome初始化代码:
if not browser:
    service = Service(ChromeDriverManager().install())
    browser = webdriver.Chrome(
        service=service,
        options=self.chrome_options,
    )

额外优化点

  • 修复__init__方法中的文档字符串缺失引号问题:将""A method to deal with browser opening""改为"""A method to deal with browser opening"""
  • 调整time_out参数值:WebDriverWait的超时单位是秒,1000秒过于冗长,建议改为10或20
  • 新增self.open_window()调用:原代码未触发URL加载,需在self.login()前添加该方法调用

修改后的__init__方法关键部分:

class BrowserAutomation(object):
    def __init__(self, browser=None, time_out=10):
        """A method to deal with browser opening"""
        self.BASE_URL ="https://erita.rita.go.tz/auth"

        if not browser:
            service = Service(ChromeDriverManager().install())
            browser = webdriver.Chrome(
                service=service,
                options=self.chrome_options,
            )

            handles = browser.window_handles
            for _, handle in enumerate(handles):
                if handle != browser.current_window_handle:
                    browser.switch_to.window(handle)
                    browser.close()
        
        self.browser = browser
        self.wait = WebDriverWait(self.browser, time_out)
        self.cli()
        self.open_window()
        self.login()

完成以上修改后,运行automate.py即可正常打开Chrome窗口并执行后续自动化操作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 02:14:58