我在使用Selenium Python绑定的测试代码中遇到此错误:
> 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 args?
__init__()
您应该问的问题 不是 “为什么要占用3个参数”,而是“ 什么 要占用3个参数”。您的回溯是指代码中非常特定的一行,而这正是问题所在。
根据此处的Selenium Python文档,selenium.webdriver.support.expected_conditions.visibility_of_element_located应当使用一个元组来调用;它不是一个函数,但实际上是一个类,其 初始化程序 只需要隐式参数之外的1个参数self:
selenium.webdriver.support.expected_conditions.visibility_of_element_located
self
class visibility_of_element_located(object): # ... def __init__(self, locator): # ...
因此,您需要visibility_of_element_located使用两个嵌套括号来调用:
visibility_of_element_located
wait.until(EC.visibility_of_element_located( ( By.CSS_SELECTOR, TWITTER_CAMPAIGNS ) ))
这意味着,而不是3个参数self,By.CSS_SELECTOR并且TWITTER_CAMPAIGNS,在visibility_of_element_located.__init__将与刚预计2个参数调用:隐式self和定位:一个(type, expression)元组。
By.CSS_SELECTOR
TWITTER_CAMPAIGNS
visibility_of_element_located.__init__
(type, expression)