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

TypeError: __init__()缺失必填参数timeout Selenium测试报错求助

报错修复方案

错误根因

你遇到的TypeError: __init__() missing 1 required positional argument: 'timeout'报错和父类构造初始化逻辑无关,问题出在GenricFun.py中的Selenium工具调用:
Selenium内置的WebDriverWait类构造要求必须传入**超时时间(timeout)**作为第二个位置参数,你当前调用时仅传入了driver一个参数,因此触发参数缺失报错。

修复步骤

  1. 修正GenricFun.py中所有WebDriverWait的调用,补充超时时间参数(单位为秒,可根据需求自行调整数值)
  2. (可选优化)home类已经继承自Pages父类,无需在logg方法中重复实例化Pages对象,直接调用继承的父类方法即可,减少冗余代码。

修改后代码示例

GenricFun.py

# 注意确保你已经提前导入了对应依赖:WebDriverWait、expected_conditions、By等
class Pages:
    def __init__(self,driver):
        self.driver=driver

    def click1(self,locator):
        # 新增超时时间参数,此处设置为10秒
        WebDriverWait(self.driver, 10).until(e.visibility_of_element_located(locator)).click()

    def type(self,locator,txt):
       # 新增超时时间参数,此处设置为10秒
       WebDriverWait(self.driver, 10).until(e.visibility_of_element_located(locator)).send_keys(txt)

home.py(优化后)

class home(Pages):
    user = (By.NAME, "user-name")
    utxt = "standard_user"
    pwd = (By.ID, "password")
    ptxt = "secret_sauce"
    logbtn = (By.NAME, "login-button")
    def __init__(self,driver):
        self.driver=driver
        super().__init__(self.driver)
    def logg(self):
        # 直接调用父类继承的方法,无需重复实例化Pages
        self.type(self.user,self.utxt)
        self.type(self.pwd,self.ptxt)
        self.click1(self.logbtn)

修改完成后重新运行测试即可解决参数缺失报错。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 20:09:00