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

继承Selenium webdriver时遇TypeError参数过多问题求助

解决Selenium继承webdriver时的TypeError问题

错误原因

你直接继承了webdriver(或selenium.webdriver)模块,但模块无法作为父类被继承,Python尝试将模块当作类实例化时,就会抛出TypeError: module() takes at most 2 arguments (3 given)错误。

修复方案

应继承具体的浏览器驱动类(比如webdriver.Chrome)而非整个模块,同时调整初始化逻辑——继承驱动类后,self本身就是浏览器驱动实例,无需额外创建self.driver。

修正后的类文件代码

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

class Exploring(webdriver.Chrome):
    def __init__(self):
        # 配置Chrome启动参数
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_experimental_option("detach", True)
        # 调用父类Chrome的初始化方法,传入配置
        super().__init__(options=chrome_options)
        # 直接通过self打开目标页面
        self.get('https://html-classic.itch.zone/html/7281144/dungeon-crawler-od-v1.1.3-browser/index.html')

    def startup(self):
        # 直接用self调用元素查找方法,self就是Chrome驱动实例
        input_name = self.find_element(By.XPATH, '//*[@id="name-input"]')
        input_name.click()
        time.sleep(1)
        input_name.send_keys("Vacine")

main.py代码

from your_class_file import Exploring  # 替换为你的类文件名

explore = Exploring()
explore.startup()

关键说明

  • 继承目标要准确:webdriver是封装各类驱动的模块,webdriver.Chrome才是可实例化的类,能作为父类继承。
  • 移除冗余实例:继承后self本身就是Chrome驱动对象,所有驱动方法(如get、find_element)可直接通过self调用。
  • 正确传递配置:在父类初始化时传入chrome_options,确保浏览器按预期配置启动。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 21:46:00