此处抛出 TypeError 异常:init() 接受 2 个位置参数,但 3 个是通过 Selenium 和 Python 使用显式等待给出的

TypeError exception was thrown here: init() takes 2 positional arguments but 3 were given using explicit wait through Selenium and Python

我从 selenium python 文档中复制了一个片段。 当然,我手动输入它以验证它是否与我正在使用的 python 版本的 Python 3.8.6 过时。 还好没有过时。 但是这里抛出了 TypeError 异常: init() 接受 2 个位置参数,但给出了 3 个

我的代码如下:(最后一行出现错误)

from selenium import webdriver
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.Firefox()
driver.get("https://www.liaoxuefeng.com/wiki/1016959663602400/1017602696742912")

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable(By.ID, 'brand'))

element_to_be_clickable() 应该在 tuple 中调用,因为它不是 函数 而是 class,其中初始化程序只需要 1 参数,超出 implicit self。实际上,您的代码将是:

wait = WebDriverWait(driver, 10)
element = wait.until((EC.element_to_be_clickable(By.ID, 'brand'))

参考

您可以在以下位置找到一些相关的详细讨论: