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

Python Selenium实现digitalwallonia翻页遇ElementNotInteractableException问题求助

解决Selenium中ElementNotInteractableException问题(浏览Digital Wallonia地图页面)

问题背景

尝试通过Python Selenium批量浏览https://www.digitalwallonia.be/fr/cartographie/的448个页面时,触发以下错误:

selenium.common.exceptions.ElementNotInteractableException: Message: Element could not be scrolled into view

原代码如下:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.implicitly_wait(20)


browser.get('https://www.digitalwallonia.be/fr/cartographie')
browser.find_element("xpath",'//*[@id="axeptio_btn_acceptAll"]').click()
browser.find_element("xpath",'//*[@id="axeptio_btn_configure"]').click()
browser.find_element("xpath",'//*[@id="axeptio_btn_acceptAllAndNext"]').click()

WebDriverWait(browser, 1000).until(EC.element_to_be_clickable((By.CLASS_NAME,'next'))).click()

input('Press ENTER to close the automated browser')
browser.quit()

解决方案

错误原因是"next"按钮未完全进入可视区域,或被页面元素遮挡。可以通过以下方式修复:

方案1:用JavaScript滚动并点击按钮

显式等待按钮可见后,通过JavaScript将元素滚动到视图中心,再执行点击:

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

browser = webdriver.Firefox()
browser.implicitly_wait(20)

browser.get('https://www.digitalwallonia.be/fr/cartographie')
# 处理Cookie弹窗
browser.find_element("xpath",'//*[@id="axeptio_btn_acceptAll"]').click()
browser.find_element("xpath",'//*[@id="axeptio_btn_configure"]').click()
browser.find_element("xpath",'//*[@id="axeptio_btn_acceptAllAndNext"]').click()

# 等待next按钮可见
next_btn = WebDriverWait(browser, 30).until(EC.visibility_of_element_located((By.CLASS_NAME, 'next')))
# 用JavaScript滚动到按钮位置(确保完全可视)
browser.execute_script("arguments[0].scrollIntoView({block: 'center'});", next_btn)
# 用JavaScript点击按钮,避免滚动交互问题
browser.execute_script("arguments[0].click();", next_btn)

input('Press ENTER to close the automated browser')
browser.quit()

方案2:模拟滚动到按钮位置后点击

如果偏好原生Selenium方法,可以先滚动页面到按钮附近,再点击:

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

browser = webdriver.Firefox()
browser.implicitly_wait(20)

browser.get('https://www.digitalwallonia.be/fr/cartographie')
# 处理Cookie弹窗
browser.find_element("xpath",'//*[@id="axeptio_btn_acceptAll"]').click()
browser.find_element("xpath",'//*[@id="axeptio_btn_configure"]').click()
browser.find_element("xpath",'//*[@id="axeptio_btn_acceptAllAndNext"]').click()

# 等待next按钮可交互
next_btn = WebDriverWait(browser, 30).until(EC.element_to_be_clickable((By.CLASS_NAME, 'next')))
# 滚动到按钮位置(预留顶部间距避免被导航栏遮挡)
browser.execute_script("window.scrollTo(0, arguments[0].offsetTop - 100);", next_btn)
# 点击按钮
next_btn.click()

input('Press ENTER to close the automated browser')
browser.quit()

批量翻页注意事项

如果要遍历448个页面,需要:

  • 加入循环逻辑,每次翻页后等待页面加载完成(比如等待列表元素更新)
  • 处理可能的重复弹窗或加载延迟
  • 加入异常捕获,避免单页失败导致整个流程中断

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 15:25:18