I am getting this error in my test code that uses Selenium Python Bindings:
我在使用Selenium Python Bindings的测试代码中收到此错误:
> twitter_campaigns = wait.until(EC.visibility_of_element_located(By.CSS_SELECTOR, TWITTER_CAMPAIGNS))
E TypeError: __init__() takes exactly 2 arguments (3 given)
And this is what Im executing:
这就是我执行的:
class TestTwitter(TestLogin, TestBuying):
def setup(self, timeout=10):
self.driver = webdriver.Firefox()
self.driver.get(BASEURL)
self.driver.implicitly_wait(timeout)
def test_campaigns_loaded(self, timeout=10):
self.signin_action()
self.view_twitter_dashboard()
self.select_brand()
wait = WebDriverWait(self.driver, timeout)
twitter_campaigns = wait.until(EC.visibility_of_element_located(By.CSS_SELECTOR, TWITTER_CAMPAIGNS))
assert True == twitter_campaigns
def teardown(self):
self.driver.close()
So I'm wondering Why Im getting the above errors, on all the classes I haven't defined an __init__()
method instead I defined a setUp and tearDown methods as pytest follow. Any ideas why is taking 3 args?
所以我想知道为什么我得到上面的错误,在我没有定义__init __()方法的所有类中,我将setUp和tearDown方法定义为pytest follow。任何想法为什么要采取3 args?
1 个解决方案
#1
15
The question you should be asking is not "why is it taking 3 args", but "what is taking 3 args". Your traceback refers to a very specific line in code, and it is there where the problem lies.
你应该问的问题不是“为什么它采取3 args”,而是“什么是3 args”。您的回溯是指代码中的一个非常具体的行,它存在问题所在。
According to the Selenium Python docs here, the selenium.webdriver.support.expected_conditions.visibility_of_element_located
should be called with a tuple; it is not a function, but actually a class, whose initializer expects just 1 argument beyond the implicit self:
根据这里的Selenium Python文档,应该使用元组调用selenium.webdriver.support.expected_conditions.visibility_of_element_located;它不是一个函数,而是一个实际上是一个类,它的初始化程序只需要一个超出隐式self的参数:
class visibility_of_element_located(object):
# ...
def __init__(self, locator):
# ...
Thus, you need to call the visibility_of_element_located
with two nested parentheses:
因此,您需要使用两个嵌套括号调用visibility_of_element_located:
wait.until(EC.visibility_of_element_located( ( By.CSS_SELECTOR, TWITTER_CAMPAIGNS ) ))
Which means that instead of 3 arguments self
, By.CSS_SELECTOR
and TWITTER_CAMPAIGNS
, the visibility_of_element_located.__init__
will be invoked with just expected 2 arguments: the implicit self
and the locator: a (type, expression)
tuple.
这意味着,而不是3个参数self,By.CSS_SELECTOR和TWITTER_CAMPAIGNS,visibility_of_element_located .__ init__将使用预期的2个参数调用:隐式self和定位器:a(type,expression)元组。
#1
15
The question you should be asking is not "why is it taking 3 args", but "what is taking 3 args". Your traceback refers to a very specific line in code, and it is there where the problem lies.
你应该问的问题不是“为什么它采取3 args”,而是“什么是3 args”。您的回溯是指代码中的一个非常具体的行,它存在问题所在。
According to the Selenium Python docs here, the selenium.webdriver.support.expected_conditions.visibility_of_element_located
should be called with a tuple; it is not a function, but actually a class, whose initializer expects just 1 argument beyond the implicit self:
根据这里的Selenium Python文档,应该使用元组调用selenium.webdriver.support.expected_conditions.visibility_of_element_located;它不是一个函数,而是一个实际上是一个类,它的初始化程序只需要一个超出隐式self的参数:
class visibility_of_element_located(object):
# ...
def __init__(self, locator):
# ...
Thus, you need to call the visibility_of_element_located
with two nested parentheses:
因此,您需要使用两个嵌套括号调用visibility_of_element_located:
wait.until(EC.visibility_of_element_located( ( By.CSS_SELECTOR, TWITTER_CAMPAIGNS ) ))
Which means that instead of 3 arguments self
, By.CSS_SELECTOR
and TWITTER_CAMPAIGNS
, the visibility_of_element_located.__init__
will be invoked with just expected 2 arguments: the implicit self
and the locator: a (type, expression)
tuple.
这意味着,而不是3个参数self,By.CSS_SELECTOR和TWITTER_CAMPAIGNS,visibility_of_element_located .__ init__将使用预期的2个参数调用:隐式self和定位器:a(type,expression)元组。