Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
676 views
in Technique[技术] by (71.8m points)

python - Selenium - visibility_of_element_located: __init__() takes exactly 2 arguments (3 given)

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()

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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):
       # ...

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 ) ))

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...