简体   繁体   中英

__init__() takes 2 positional arguments but 3 were given using WebDriverWait and expected_conditions as element_to_be_clickable with Selenium Python

I saw similar questions but in my case there is not even "init" function in my code. How to solve this problem? The problem is with line (EC.element_to_bo_clickable)

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(executable_path="C:\Chromedriver\chromedriver.exe")
driver.implicitly_wait(1)
driver.get("https://cct-103.firebaseapp.com/")

element = WebDriverWait(driver, 1).until
(EC.element_to_be_clickable(By.CLASS_NAME, "MuiButton-label"))

element.click()

According to the definition, element_to_be_clickable() should be called within a tuple as it is not a function but a class , where the initializer expects just 1 argument beyond the implicit self :

class element_to_be_clickable(object):
    """ An Expectation for checking an element is visible and enabled such that you can click it."""
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        element = visibility_of_element_located(self.locator)(driver)
        if element and element.is_enabled():
            return element
        else:
            return False

So instead of:

element = WebDriverWait(driver, 1).until(EC.element_to_be_clickable(By.CLASS_NAME, "MuiButton-label"))

You need to (add an extra parentheses):

element = WebDriverWait(driver, 1).until((EC.element_to_be_clickable(By.CLASS_NAME, "MuiButton-label")))

You are missing: () , in this argument: ((By.CLASS_NAME, "MuiButton-label")) .

Try the bellow code:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(executable_path="C:\Chromedriver\chromedriver.exe")
driver.get("https://cct-103.firebaseapp.com/")

element = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CLASS_NAME, "MuiButton-label")))
element.click()

element_to_be_clickable() calls internally to visibility_of_element_located

def __call__(self, driver):
    element = visibility_of_element_located(self.locator)(driver)

Which call _find_element

def __call__(self, driver):
    try:
        return _element_if_visible(_find_element(driver, self.locator))

Which in turn call to find_element(self, by=By.ID, value=None) in webdriver.py

def _find_element(driver, by):
    try:
        return driver.find_element(*by)

This means you need to send a tuple as parameter

element = WebDriverWait(driver, 1).until(EC.element_to_be_clickable((By.CLASS_NAME, "MuiButton-label")))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM