Selenium Python无法点击搜索按钮问题求助
问题:Selenium无法点击欧盟资助搜索页面的搜索按钮
我正在编写代码实现搜索栏输入内容并点击搜索按钮的功能:
- 搜索栏输入内容的部分运行正常;
- 但点击搜索按钮的部分始终无法实现,查了大量资料、试了好几个小时,所有方法都没用……
目标网站:https://ec.europa.eu/info/funding-tenders/opportunities/portal/screen/opportunities/topic-search
搜索按钮的HTML结构:
<button type="submit" id="f2q3fi9kd" class="btn ux-button btn-accent btn-lg ux-button--large ux-button--icon-only" tabindex="0" aria-disabled="false" data-e2e="ux-button">
初始代码:
(xxx为需要填写的按钮定位代码)
from selenium import webdriver from bs4 import BeautifulSoup as soup from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver import ActionChains from selenium.webdriver.common.keys import Keys cc_driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install())) cc_driver.get('https://ec.europa.eu/info/funding-tenders/opportunities/portal/screen/opportunities/topic-search') action = ActionChains(cc_driver) searchTextbox = WebDriverWait(cc_driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "input[placeholder='Type your Keywords...']" ))) action.move_to_element(searchTextbox).click().send_keys("CREA-CROSS-2023-JOURPART-PLURALISM").perform() save_Button = WebDriverWait(cc_driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'xxx'))) action.move_to_element(save_Button).click()
更新后的尝试代码:
from selenium import webdriver from bs4 import BeautifulSoup as soup from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver import ActionChains from selenium.webdriver.common.keys import Keys cc_driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install())) cc_driver.get('https://ec.europa.eu/info/funding-tenders/opportunities/portal/screen/opportunities/topic-search') action = ActionChains(cc_driver) # 输入搜索内容 searchTextbox = WebDriverWait(cc_driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "input[placeholder='Type your Keywords...']" ))) action.move_to_element(searchTextbox).click().send_keys("CREA-CROSS-2023-JOURPART-PLURALISM").perform() # 尝试1 save_Button = WebDriverWait(cc_driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, 'icon-search.ux-u-font-size-h2'))) cc_driver.execute_script("arguments[0].click();", save_Button) # 尝试2 save_Button = WebDriverWait(cc_driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, 'icon-search.ux-u-font-size-h2'))) action.move_to_element(save_Button).click() cc_driver.execute_script("arguments[0].click();", save_Button) # 尝试3 save_Button = WebDriverWait(cc_driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, 'icon-search.ux-u-font-size-h2'))) save_Button.click() # 尝试4 save_Button = WebDriverWait(cc_driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, 'icon-search.ux-u-font-size-h2'))) save_Button.click() cc_driver.execute_script("arguments[0].click();", save_Button)
解决方案
你的问题核心是元素定位错误,以下是可行的修复方案:
1. 正确定位搜索按钮
你之前用By.CLASS_NAME定位icon-search.ux-u-font-size-h2是错误的:
CLASS_NAME不支持多类名(.分隔的多个类);- 这个类属于按钮内部的图标元素,而非按钮本身。
根据提供的HTML,推荐两种可靠定位方式:
- 方案A:用
data-e2e属性定位(e2e标识一般不会轻易变动)
save_Button = WebDriverWait(cc_driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[data-e2e="ux-button"]')))
- 方案B:用按钮的类组合+类型定位
save_Button = WebDriverWait(cc_driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.btn.ux-button.btn-accent.btn-lg.ux-button--large.ux-button--icon-only[type="submit"]')))
2. 简化交互逻辑
搜索场景下,直接给搜索框发送回车键比点击按钮更稳定,可替代按钮点击:
searchTextbox.send_keys(Keys.ENTER)
完整修复代码
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.common.keys import Keys cc_driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install())) cc_driver.get('https://ec.europa.eu/info/funding-tenders/opportunities/portal/screen/opportunities/topic-search') # 方式1:输入内容后回车搜索(推荐) searchTextbox = WebDriverWait(cc_driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[placeholder='Type your Keywords...']"))) searchTextbox.click() searchTextbox.send_keys("CREA-CROSS-2023-JOURPART-PLURALISM") searchTextbox.send_keys(Keys.ENTER) # 方式2:定位按钮后点击(二选一即可) # save_Button = WebDriverWait(cc_driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[data-e2e="ux-button"]'))) # save_Button.click()
额外提示
- 优先用
EC.element_to_be_clickable代替presence_of_element_located,确保元素可交互; - 避免重复定位同一元素,浪费资源;
- 若页面有Cookie弹窗等遮挡元素,需先处理弹窗再操作。
内容的提问来源于stack exchange,提问作者pepouse
相关产品推荐
相关产品推荐

