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

Python Selenium类实例中滚动失效引发元素点击拦截问题

Selenium滚动元素后点击失败:控制台可行,类方法内无效

问题背景

我正在编写Python Selenium脚本从网站下载账单,当账单行数超出视口范围后,脚本执行失败。

已尝试的滚动方案

  • ActionChains(self.driver).move_to_element(element).perform() → 无效
  • element.location_once_scrolled_into_view → 在PyCharm控制台执行有效
  • self.driver.execute_script("window.scrollTo({});") → 在PyCharm控制台执行有效

异常情况

方案2和3在PyCharm控制台逐行执行时表现正常:侧边栏会滚动到目标元素位置,元素也能正常点击。但将代码放入类实例的downloader方法中运行时,滚动条完全不移动,同时抛出以下错误:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (1187, 642)

相关代码片段

def downloader(self, count, provider):
    print("count: ", count)  # count是对应服务商的账单行数
    for number in range(1, (count + 1)) :
        #sleep(2)
        element = self.wait.until(EC.visibility_of(
                          self.driver.find_element(By.XPATH, 
                       "//table/tbody/tr[{}]/td[6]".format(number)))) 
        location = element.location_once_scolled_into_view
        # ActionChains(self.driver).move_to_element(element).perform()
        #self.driver.execute_script("arguments[0].scrollIntoView(true);",element);
        # self.wait.until(EC.element_to_be_clickable(element))
        # sleep(1) 
        self.driver.execute_script("window.scrollTo({});".format(location))
        element.click()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 00:05:47