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

Selenium报错move_to requires a WebElement无法点击第二个加载更多按钮

报错原因
  • 你使用find_elements_by_xpath定位「加载更多」按钮,该方法返回的是WebElement列表,而move_to_element要求传入单个WebElement对象,传入列表直接触发属性错误
  • 硬编码滚动高度的逻辑适配性差,页面加载新商品后总高度会动态变化,很容易出现按钮未进入可视区就触发点击的问题
  • 第一次点击「加载更多」后,原按钮DOM会被页面销毁替换,复用旧元素会直接报错
修复方案

修改核心逻辑:改用单个元素定位方法,滚动到元素位置再点击,优化循环判断条件,同时替换Selenium已废弃的定位API,修复后的代码如下:

import xlwt
from selenium import webdriver
import time
from datetime import date
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

class tronymobiles:
    def __init__(self):
        self.url='https://www.trony.it/online/telefonia/smartphone-cellulari/smartphone_ct-VHJvbnktQjJDLVRyb255fHx8MzE5OQ'       
        self.country='IT'
        self.currency='euro'
        self.VAT='Included'
    def trony(self):
        wb = xlwt.Workbook()
        ws = wb.add_sheet('Sheet1',cell_overwrite_ok=True)
        # 修复原表头重复写同一单元格的问题
        ws.write(0,0,"Product_Manufacturer")
        ws.write(0,1,"Product_Url")
        ws.write(0,2,"Product_Price")
        ws.write(0,3,"Product_Model")
        ws.write(0,4,"Memory")
        ws.write(0,5,"Currency")
        ws.write(0,6,"Color")
        ws.write(0,7,"VAT")
        ws.write(0,8,"Shipping Cost")
        ws.write(0,9,"Pre-PromotionPrice")
        ws.write(0,10,"Country")
        ws.write(0,11,"Date")
        ws.write(0,12,"Raw_Model")
        wb.save(r"C:\Users\Karthick R\Desktop\VS code\trony.xls")
        driver=webdriver.Chrome()            
        driver.get(self.url)
        today = date.today()            
        driver.maximize_window()
        time.sleep(3)
        # 替换为Selenium4+推荐的定位写法
        overlay_close_button = driver.find_element(By.CSS_SELECTOR, 'div[class="kor-overlay-close kor-control"]')         
        overlay_close_button.click()
        try:
            time.sleep(2)
            cookies = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'ACCETTO')]")))
            cookies.click()
            print("Accepted cookies")
        except:
            pass
        
        # 优化加载更多逻辑,最多点击次数可根据需求调整
        max_click_times = 5
        click_count = 0
        while click_count < max_click_times:
            try:
                # 等待加载更多按钮可点击
                load_more_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, 'container-link ShowMoreProductsEnt')]")))
                # 滚动到按钮位置后点击
                ActionChains(driver).move_to_element(load_more_btn).pause(1).click().perform()
                click_count += 1
                print(f"第{click_count}次点击加载更多成功")
                # 等待新商品加载完成
                time.sleep(4)
            except Exception as e:
                print("没有更多商品可加载,退出循环:", str(e))
                break
        
        # 提取商品信息
        containers = driver.find_elements(By.CSS_SELECTOR, 'div[class="smcc-listing-risultati-prodotto container-fluid false"]')                 
        for index, container in enumerate(containers, start=1):                      
            try:
                url = container.find_element(By.CSS_SELECTOR, '.listing_risultati_prodotto_2 a').get_attribute('href')
                print(url)
                title = container.find_element(By.CSS_SELECTOR, '.listing_risultati_prodotto_2').text
                print(title)
                # 补充写入Excel的逻辑,原代码只会保存空表
                ws.write(index, 1, url)
                ws.write(index, 0, title.split(" ")[0] if title else "")
                ws.write(index, 12, title)
            except Exception as e:
                print(f"提取第{index}个商品出错:", str(e))
                continue
        # 最终保存Excel
        wb.save(r"C:\Users\Karthick R\Desktop\VS code\trony.xls")
        driver.quit()
        
trony_it=tronymobiles()
trony_it.trony()
额外说明
  • 修复了原代码表头重复写入同一单元格的问题,避免表头被覆盖
  • 你可以根据实际需求调整max_click_times参数,控制加载更多的次数,当前代码如果检测不到加载更多按钮会自动停止
  • 所有元素定位都替换为Selenium 4+官方推荐的写法,避免后续版本升级出现API废弃报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 20:18:03