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

如何在Selenium中点击HTML的button元素?拦截问题求解

解决Selenium元素点击被拦截的问题

错误原因

你遇到的ElementClickInterceptedException是因为页面上的弹窗图片(<img src="https://d2g38dx0j6xqav.cloudfront.net/online/img/app-inner/popup.png">)完全覆盖了目标按钮,导致实际点击被这个弹窗元素接收,而非你要点击的按钮。

解决方案

1. 先隐藏遮挡的弹窗元素

直接通过JavaScript隐藏遮挡的图片,再点击目标按钮:

# 定位遮挡的弹窗图片
popup_img = driver.find_element(By.XPATH, "//img[@src='https://d2g38dx0j6xqav.cloudfront.net/online/img/app-inner/popup.png']")
# 用JS隐藏它
driver.execute_script("arguments[0].style.display = 'none';", popup_img)

# 精准定位第一个带X的按钮
target_button = driver.find_element(By.XPATH, "//button[@class='el-button yellowBtn el-button--default']")
target_button.click()

2. 用JavaScript直接点击目标按钮

绕开元素可见性和遮挡的限制,直接触发按钮的点击事件:

# 精准定位第一个按钮
target_button = driver.find_element(By.XPATH, "//button[contains(@class, 'yellowBtn') and @type='button']")
# 用JS执行点击
driver.execute_script("arguments[0].click();", target_button)

3. 等待弹窗自动消失后再点击

如果弹窗是临时加载的,等待它从页面中消失后再操作:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 等待弹窗图片不可见,最多等10秒
WebDriverWait(driver, 10).until(
    EC.invisibility_of_element_located((By.XPATH, "//img[@src='https://d2g38dx0j6xqav.cloudfront.net/online/img/app-inner/popup.png']"))
)

# 然后点击目标按钮
driver.find_element(By.XPATH, "//button[@class='el-button yellowBtn el-button--default']").click()

第二个按钮的点击参考

解决第一个按钮后,第二个按钮可以复用类似思路:

  • 先确认页面无遮挡元素
  • 用精准定位器(比如根据它的class或显示文本),再选择普通点击或JS点击方式

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 09:21:35