You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Selenium报错TypeError: element_to_be_clickable() takes 1 positional argument?

报错解决说明

报错原因

EC.element_to_be_clickable() 方法仅支持接收1个位置参数,合法的入参格式有两种:

  • 已经完成定位的Selenium WebElement对象
  • 由定位规则(By.xxx)和选择器内容组成的元组

你当前直接将By.CSS_SELECTOR和#add-flight-switch作为两个独立参数传入方法,不符合入参格式要求,因此触发类型错误。

修复方法

将两个定位参数打包为元组后再传入element_to_be_clickable()方法即可,同时你代码存在两处细节问题需要同步修正:缺少Selenium webdriver导入、XPath定位语句缺少全局匹配前缀。

关键修改代码

  1. 等待可点击元素的行:
# 两层括号:内层括号用于将定位参数打包为元组
elem = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#add-flight-switch")))
  1. 定位出发地按钮的行:
# 新增//作为XPath全局匹配前缀
elem1 = driver.find_element(By.XPATH, "//button[aria-label='Leaving from']").text

修正后完整可运行代码

from selenium import webdriver
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()

内容的提问来源于stack exchange,提问作者Mikey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 23:15:00