[Selenium]webdriver.until() TypeError: __init__() takes 2 positional arguments but 3 were given_解决方案

异常描述:
执行以下代码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
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值