如何解决TypeError:element_to_be_clickle()占用1个位置参数



当我运行以下代码时,我得到了错误TypeError: element_to_be_clickable() takes 1 positional argument but 2 were given

from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
url = 'https://www.expedia.co.uk/'
s = Service(ChromeDriverManager().install())
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
driver = webdriver.Chrome(service=s, options=chrome_options)
driver.get(url)
elem = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(By.CSS_SELECTOR, "#add-flight-switch"))
elem.click()
elem1 = driver.find_element(By.XPATH, "button[aria-label='Leaving from']").text
driver.quit()

我可以看到elements_to_be_clickable()函数中确实有两个参数,但By.CSS_SELECTOR部分是否不需要通过特定的选择器类型(在本例中为CSS(来查找??

我正在使用选择器集线器获取CSS_selector信息。

我们将非常感谢对如何解决问题的任何支持。

您确实需要By和值,但函数希望在tuple中将两个值打包为一

EC.element_to_be_clickable((By.CSS_SELECTOR, "#add-flight-switch"))

如果您深入查看源代码,您将看到element_to_be_clickable在内部使用_find_element,此函数将在driver.find_element()中使用的tuple解压缩

class element_to_be_clickable(object):
def __init__(self, locator):
self.locator = locator
def __call__(self, driver):
element = visibility_of_element_located(self.locator)(driver)
...
class visibility_of_element_located(object):
def __init__(self, locator):
self.locator = locator
def __call__(self, driver):
return _element_if_visible(_find_element(driver, self.locator))
...
def _find_element(driver, by):
...
return driver.find_element(*by)
...

前面的答案很好,我想说的是,您只需要按照函数的期望添加一些括号。它对我有效。

elem = WebDriverWait(driver,10).until((EC.element_to_be_clickable(By.CSS_SELECTOR, "#add-flight-switch")))

相关内容

  • 没有找到相关文章

最新更新