硒-能见度_元素_定位:__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)
这就是我正在执行的操作:
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()
所以我想知道为什么为什么在所有类上我都没有定义上述__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 文档,
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__
将使用仅预期的2个参数调用:而不是3个参数self
,By.CSS_SELECTOR
和TWITTER_CAMPAIGNS
:隐式的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.
这篇关于硒-能见度_元素_定位:__init __()恰好接受2个参数(给定3个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!