Selenium -visibility_of_element_located: __init__() 需要 2 个参数(给定 3 个) [英] Selenium - visibility_of_element_located: __init__() takes exactly 2 arguments (3 given)
问题描述
我在使用 Selenium Python 绑定的测试代码中收到此错误:
I am getting this error in my test code that uses 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)
这就是我正在执行的:
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()
所以我想知道为什么我会收到上述错误,在所有类上我都没有定义 __init__()
方法,而是在 pytest 之后定义了 setUp 和 tearDown 方法.知道为什么要使用 3 个参数吗?
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?
推荐答案
您应该问的问题是不是为什么需要 3 个参数",而是什么> 需要 3 个参数".您的回溯指的是代码中非常具体的一行,问题就在那里.
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.
根据此处的 Selenium Python 文档,condition_codes_element.expectibility
condition_codes.expectibility. 应该用元组调用;它不是一个函数,而是一个类,它的初始化器只需要 1 个超出隐式 self
的参数:
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
:
class visibility_of_element_located(object):
# ...
def __init__(self, locator):
# ...
因此,您需要使用两个嵌套括号调用visibility_of_element_located
:
Thus, you need to call the visibility_of_element_located
with two nested parentheses:
wait.until(EC.visibility_of_element_located( ( By.CSS_SELECTOR, TWITTER_CAMPAIGNS ) ))
这意味着 visibility_of_element_located.__init__
而不是 3 个参数 self
、By.CSS_SELECTOR
和 TWITTER_CAMPAIGNS
将仅使用预期的 2 个参数调用:隐式 self
和定位器:(type, expression)
元组.
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.
这篇关于Selenium -visibility_of_element_located: __init__() 需要 2 个参数(给定 3 个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!