TypeError: __init__()缺失必填参数timeout Selenium测试报错求助
报错修复方案
错误根因
你遇到的TypeError: __init__() missing 1 required positional argument: 'timeout'报错和父类构造初始化逻辑无关,问题出在GenricFun.py中的Selenium工具调用:
Selenium内置的WebDriverWait类构造要求必须传入**超时时间(timeout)**作为第二个位置参数,你当前调用时仅传入了driver一个参数,因此触发参数缺失报错。
修复步骤
- 修正
GenricFun.py中所有WebDriverWait的调用,补充超时时间参数(单位为秒,可根据需求自行调整数值) - (可选优化)
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
相关产品推荐
相关产品推荐

