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

Python Selenium无法点击id=aberto-btn的网页按钮求助

问题:Selenium无法点击页面中的"Abertos"按钮(id=aberto-btn)

此前代码可正常爬取页面https://recuperarportugal.gov.pt/candidaturas-prr/,但现在使用Selenium无法点击id为aberto-btn的"Abertos"按钮,尝试过Actions、XPath、选择器等方法均失败,报错ElementClickInterceptedException。

原代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager 
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

opciones=Options()
opciones.add_experimental_option('excludeSwitches', ['enable-automation'])
opciones.add_experimental_option('useAutomationExtension', False)
opciones.add_argument('--no-sandbox')
opciones.add_argument('--disable-dev-shm-usage')
opciones.headless=True

PATH=ChromeDriverManager().install() 

URL ='https://recuperarportugal.gov.pt/candidaturas-prr/'
driver = webdriver.Chrome(executable_path='/usr/lib/chromium-browser/chromedriver',chrome_options=opciones)

driver.get(URL)
time.sleep(5)
driver.maximize_window()
wait = WebDriverWait(driver, 30)

driver.find_element("id","aberto-btn").click()

报错信息

---------------------------------------------------------------------------
ElementClickInterceptedException          Traceback (most recent call last)
<ipython-input-22-05303687610b> in <module>
     13 
     14 
---> 15 driver.find_element("id","aberto-btn").click()
     16 
     17 time.sleep(2)

3 frames
/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    241                 alert_text = value['alert'].get('text')
    242             raise exception_class(message, screen, stacktrace, alert_text)  # type: ignore[call-arg]  # mypy is not smart enough here
---> 243         raise exception_class(message, screen, stacktrace)

ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (574, 983)
  (Session info: headless chrome=104.0.5112.101)
Stacktrace:
#0 0x55c08f46b383 <unknown>
#1 0x55c08f23c353 <unknown>
#2 0x55c08f27b531 <unknown>
#3 0x55c08f279134 <unknown>
#4 0x55c08f27671e <unknown>
#5 0x55c08f275574 <unknown>
#6 0x55c08f269d2a <unknown>
#7 0x55c08f291db2 <unknown>
#8 0x55c08f2695c6 <unknown>
#9 0x55c08f2921de <unknown>
#10 0x55c08f2a6b9c <unknown>
#11 0x55c08f292183 <unknown>
#12 0x55c08f267bfc <unknown>
#13 0x55c08f2690c5 <unknown>
#14 0x55c08f4de510 <unknown>
#15 0x55c08f4a1eb7 <unknown>
#16 0x55c08f4a1acd <unknown>
#17 0x55c08f4a2522 <unknown>
#18 0x55c08f4dad6b <unknown>
#19 0x55c08f4a276e <unknown>
#20 0x55c08f4854e3 <unknown>
#21 0x55c08f4abcb8 <unknown>
#22 0x55c08f4abe4a <unknown>
#23 0x55c08f4c5520 <unknown>
#24 0x7fcb635486db <unknown>

解决方案

ElementClickInterceptedException说明元素被其他元素遮挡,或不在当前视口内,结合无头模式的特性,可通过以下步骤解决:

1. 修正无头模式窗口尺寸

无头Chrome默认窗口很小,可能导致按钮在视口外,添加窗口大小参数:

opciones.add_argument("--window-size=1920,1080")

2. 替换强制等待为显式等待

放弃time.sleep(5),用显式等待确保元素加载完成且可交互:

button = wait.until(EC.element_to_be_clickable((By.ID, "aberto-btn")))

3. 处理页面遮挡元素

页面可能存在cookie弹窗或footer遮挡按钮,先关闭弹窗,再滚动到按钮位置:

# 关闭cookie弹窗(如果存在)
try:
    cookie_btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Aceitar')]")))
    cookie_btn.click()
except:
    pass

# 滚动到按钮位置
driver.execute_script("arguments[0].scrollIntoView(true);", button)

4. 使用JavaScript点击替代原生点击

如果元素仍无法被点击,直接用JS触发点击事件:

driver.execute_script("arguments[0].click();", button)

完整修正代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

opciones = Options()
opciones.add_experimental_option('excludeSwitches', ['enable-automation'])
opciones.add_experimental_option('useAutomationExtension', False)
opciones.add_argument('--no-sandbox')
opciones.add_argument('--disable-dev-shm-usage')
opciones.add_argument("--window-size=1920,1080")
opciones.headless = True

# 用ChromeDriverManager自动管理驱动,无需手动指定路径
driver = webdriver.Chrome(options=opciones)
URL = 'https://recuperarportugal.gov.pt/candidaturas-prr/'
driver.get(URL)
wait = WebDriverWait(driver, 30)

# 处理cookie弹窗
try:
    cookie_btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Aceitar')]")))
    cookie_btn.click()
except:
    pass

# 等待按钮加载,滚动到位置后用JS点击
button = wait.until(EC.presence_of_element_located((By.ID, "aberto-btn")))
driver.execute_script("arguments[0].scrollIntoView(true);", button)
time.sleep(1)
driver.execute_script("arguments[0].click();", button)

# 后续操作逻辑...

driver.quit()

内容的提问来源于stack exchange,提问作者Manuel Carlos Cabanillas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 00:41:03