异常描述:
执行以下代码element = WebDriverWait(driver,5,0.5).until(EC.presence_of_element_located(By.ID,“su”)),PyCharm出现异常提示TypeError: init() takes 2 positional arguments but 3 were given。
大致意思:init()方法只取2个参数,但实际提供了3个参数。
问题分析:
查看expected conditions.py模块的代码,其中找到 presence_of_element_located 类
class presence_of_element_located(object):
“”" An expectation for checking that an element is present on the DOM
of a page. This does not necessarily mean that the element is visible.
locator - used to find the element
returns the WebElement once it is located
“”"
def init(self, locator):
self.locator = locator
def __call__(self, driver):
return _find_element(driver, self.locator)
最初执行的代码一开始是这样写:presence_of_element_located(By.ID,“su”),这样相当于取到了3个参数(self, By.ID, “su”)
而presence_of_element_located类中__init__()方法取的确实是2个参数(self, locator),其中locator调用的是一个tuple(元组)
该元组(By.ID,“su”)作为一个整体,对应相当于1个参数,加上类实例化代表自身的self参数,正好就是2个参数
因此,执行代码正确的写法为:presence_of_element_located((By.ID,“su”)),即需要嵌套两层英文圆括号
解决方案
将执行代码调整为:
element = WebDriverWait(driver,5,0.5).until(EC.presence_of_element_located((By.ID,“su”)))
参考资料:
【1】Selenium - visibility_of_element_located: init() takes exactly 2 arguments (3 given)
【2】Selenium TypeError: init() takes 2 positional arguments but 3 were given [duplicate]
【3】Why are there 2 parentheses in EC.element_to_be_clickable((By.CSS_SELECTOR, “css_selector”)))?
作者:Fighting_001
链接:https://www.jianshu.com/p/f22bd90756bf
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。