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

Selenium报错element click intercepted:元素无法点击问题求助

解决Selenium点击“显示更多”按钮被拦截的问题

针对你遇到的element click intercepted: Element is not clickable at point (774, 8907)错误,结合目标页面情况,给你几个实际可行的解决思路:

1. 解决元素被底部固定栏遮挡的问题

很多网站底部会有固定导航、广告栏,滚动到按钮位置后,按钮会被这些栏挡住,导致点击失败。可以调整滚动位置,让按钮居中或往上偏移:

more_button = driver.find_element(by=By.CLASS_NAME, value='tpl-showmore-content')
# 滚动到按钮居中位置
driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", more_button)
# 再手动往上滚一点,彻底避开底部栏
driver.execute_script("window.scrollBy(0, -100);")

2. 用更严谨的等待+JS点击替代原生点击

原生click()容易受页面遮挡影响,改用JS执行点击更稳定,同时配合WebDriverWait确保元素完全可交互:

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

# 循环内修改为:
try:
    # 最多等10秒,直到按钮可点击
    more_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CLASS_NAME, 'tpl-showmore-content'))
    )
    # 先调整滚动位置
    driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", more_button)
    driver.execute_script("window.scrollBy(0, -120);")
    # 用JS点击,绕过页面交互限制
    driver.execute_script("arguments[0].click();", more_button)
    # 点击后等新内容加载,避免循环过快
    WebDriverWait(driver, 6).until(
        lambda d: len(d.find_elements(By.CLASS_NAME, 'tpl-showmore-content')) > 0
    )
except selexp.NoSuchElementException:
    err = True
    print("没有更多内容了")

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
import selenium.common.exceptions as selexp

driver = webdriver.Chrome(options=chrome_options)
driver.maximize_window()

for el in states_pages:
    driver.get(el)
    # 先等页面核心表格加载完成
    WebDriverWait(driver, 12).until(EC.presence_of_element_located((By.TAG_NAME, "table")))
    err = False
    while not err:
        try:
            more_button = WebDriverWait(driver, 10).until(
                EC.element_to_be_clickable((By.CLASS_NAME, 'tpl-showmore-content'))
            )
            # 调整滚动位置,避开底部遮挡
            driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", more_button)
            driver.execute_script("window.scrollBy(0, -120);")
            # JS点击更稳定
            driver.execute_script("arguments[0].click();", more_button)
            # 等待新内容加载完成
            WebDriverWait(driver, 6).until(
                lambda d: len(d.find_elements(By.CLASS_NAME, 'tpl-showmore-content')) > 0
            )
        except selexp.NoSuchElementException:
            err = True
            print("已加载完所有内容")
        except selexp.ElementClickInterceptedException:
            # 再次尝试调整滚动后点击
            driver.execute_script("window.scrollBy(0, -200);")
            driver.execute_script("arguments[0].click();", more_button)
        except Exception as e:
            err = True
            print(f"出错:{e}")

driver.quit()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 05:35:50