【问题标题】:TypeError: __init__() missing 1 required positional argument: 'timeout'类型错误:__init__() 缺少 1 个必需的位置参数:“超时”
【发布时间】:2021-11-30 02:49:16
【问题描述】:

我正在尝试为一个基本项目运行一些基本测试用例,但是尽管初始化了超类构造函数,但我还是得到了这个错误。下面是我的代码。

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):
        pg=Pages(self.driver)
        pg.type(self.user,self.utxt)
        pg.type(self.pwd,self.ptxt)
        pg.click1(self.logbtn)`

GenricFun.py

class Pages:
    def __init__(self,driver):
        self.driver=driver

    def click1(self,locator):
        WebDriverWait(self.driver).until(e.visibility_of_element_located(locator)).click()

    def type(self,locator,txt):
       WebDriverWait(self.driver).until((e.visibility_of_element_located(locator))).send_keys(txt)`

Test_log.py

 class Test_log(TestBase):
    def test_log(self):
        self.driver.get("https://www.saucedemo.com/")
        L=home(self.driver)
        L.logg()

test_log()中的driver来自fixtures方法(confest.py)

【问题讨论】:

  • 您能否包含您遇到的异常的完整回溯?由于您的代码不是minimal reproducible example,因此我无法直接对其进行测试,因此我不确定我是否了解错误的确切来源。您的所有类都不需要 timeout 参数,因此它可能是对库代码的调用之一,知道哪一个会有所帮助!关于你的类的主题,你在home.logg 中做了一些非常奇怪的事情,你正在创建你的基类的另一个实例。您不需要这样做,只需在 self 上调用这些方法即可!你也根本不需要home.__init__

标签: python python-3.x selenium-webdriver pytest


【解决方案1】:

问题在这里

def click1(self,locator):
        WebDriverWait(self.driver).until(e.visibility_of_element_located(locator)).click()

你缺少括号(),请使用下面的代码:

def click1(self,locator):
        WebDriverWait(self.driver).until((e.visibility_of_element_located(locator))).click()

更新:

等效的语句是:

user = (By.NAME, "user-name")
WebDriverWait(driver).until(EC.visibility_of_element_located(user)).click()

所以上面定义的方法应该是这样的:

def click1(self, locator):
    WebDriverWait(self.driver).until(EC.visibility_of_element_located(locator)).click()


def type(self, locator, txt):
    WebDriverWait(self.driver).until(EC.visibility_of_element_located(locator)).send_keys(txt)

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

【讨论】:

  • 我做了你建议的更改,但它再次显示 type () 的相同错误,我认为它可能与 python 中的 type 关键字冲突,因此将函数名称更改为 typt_txt,但我仍然在这一行出现错误@ 987654326@ TypeError: __init__() 缺少 1 个必需的位置参数:'timeout'
  • @shivani :查看上面的更新代码。
  • 我对这个答案感到非常困惑。顶部似乎非常强调一些在我看来没有做任何事情的括号(在不更改操作顺序时它们是多余的,例如在(1+2) 中)。我不确定我是否理解答案的下半部分在说什么。您可以再次编辑并尝试使答案成为一个连贯的整体吗?我们不需要在答案文本中说明您的思想演变,如果需要,我们可以浏览编辑历史记录。
猜你喜欢
  • 2022-01-11
  • 2021-07-06
  • 2023-03-19
  • 2020-01-29
  • 2017-10-30
  • 2021-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多