【问题标题】:How to resolve TypeError: element_to_be_clickable() takes 1 positional argument?如何解决 TypeError: element_to_be_clickable() 需要 1 个位置参数?
【发布时间】:2021-12-18 18:05:03
【问题描述】:

我在运行以下代码时收到错误 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 信息。

非常感谢任何有关如何解决的支持。

【问题讨论】:

  • elem = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#add-flight-switch"))) 单个参数是一个元组
  • @BrutusForcus 非常感谢您的快速响应!我确实错过了额外的括号。

标签: python selenium web-scraping selenium-chromedriver element


【解决方案1】:

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

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

如果你深入源码你会看到element_to_be_clickable在内部使用_find_element,这个函数解包tupledriver.find_element()中使用

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 2013-07-06
    • 2019-06-25
    相关资源
    最近更新 更多