Python Selenium点击Currys网站分页链接无响应问题求助
Selenium无法点击Currys电视页面下一页按钮的问题解决
针对你遇到的问题——点击下一页按钮时元素高亮但页面不跳转,结合Currys网站的特性,以下是几种可行的解决方案:
可能的核心原因
- Cookie弹窗未关闭,干扰页面交互逻辑
- 按钮元素未完全进入视口,导致点击事件未被正确触发
- 网站绑定的JS事件需要真实的鼠标交互(如移动+点击),直接调用
click()或execute_script无法触发 - 定位的
<a>标签并非实际触发跳转的元素(可能父级<li>才是可点击区域)
解决方案1:先处理Cookie弹窗
Currys的Cookie授权弹窗会占据页面焦点,甚至遮挡元素,优先关闭它:
# 等待并关闭Cookie弹窗 try: cookie_accept = WebDriverWait(driver, 5).until( ec.element_to_be_clickable((By.ID, 'onetrust-accept-btn-handler')) ) cookie_accept.click() time.sleep(2) # 给弹窗关闭的缓冲时间 except Exception as e: print("Cookie弹窗处理异常:", e)
解决方案2:滚动到按钮可见区域再点击
确保按钮完全在视口中,避免因元素不可见导致点击失效:
next_page_link = WebDriverWait(driver, 10).until( ec.element_to_be_clickable((By.CSS_SELECTOR, 'ul.pagination li.pagination-arrrow-next a.page-next')) ) # 滚动到元素居中位置 driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", next_page_link) time.sleep(1) next_page_link.click()
解决方案3:用ActionChains模拟真实鼠标操作
模拟用户的鼠标移动+点击,更贴合网站的事件触发逻辑:
from selenium.webdriver.common.action_chains import ActionChains next_page_link = WebDriverWait(driver, 10).until( ec.element_to_be_clickable((By.CSS_SELECTOR, 'ul.pagination li.pagination-arrrow-next a.page-next')) ) driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", next_page_link) time.sleep(1) # 模拟鼠标移动到元素上再点击 ActionChains(driver).move_to_element(next_page_link).click().perform()
解决方案4:直接通过href跳转(备选)
如果以上方法均无效,可直接获取按钮的href属性,调用driver.get()跳转:
next_page_link = WebDriverWait(driver, 10).until( ec.presence_of_element_located((By.CSS_SELECTOR, 'ul.pagination li.pagination-arrrow-next a.page-next')) ) next_url = next_page_link.get_attribute('href') driver.get(next_url)
完整测试代码
整合所有步骤的完整示例:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service 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.action_chains import ActionChains import time chrome_options = Options() chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options) driver.set_window_size(1920, 1080) driver.maximize_window() driver.get('https://www.currys.co.uk/tv-and-audio/televisions/tvs') # 处理Cookie弹窗 try: cookie_accept = WebDriverWait(driver, 5).until( ec.element_to_be_clickable((By.ID, 'onetrust-accept-btn-handler')) ) cookie_accept.click() time.sleep(2) except Exception as e: print("Cookie弹窗处理异常:", e) # 定位并点击下一页按钮 next_page_link = WebDriverWait(driver, 10).until( ec.element_to_be_clickable((By.CSS_SELECTOR, 'ul.pagination li.pagination-arrrow-next a.page-next')) ) driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", next_page_link) time.sleep(1) ActionChains(driver).move_to_element(next_page_link).click().perform() time.sleep(5) print("当前页面URL:", driver.current_url) driver.quit()
内容的提问来源于stack exchange,提问作者mark377
相关产品推荐
相关产品推荐

