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

如何解决Selenium点击“Load More Listings”按钮时的“Move target out of bounds”错误?

解决Selenium点击“Load More Listings”时的“Move target out of bounds”错误

问题原因

“Move target out of bounds”错误通常是因为目标按钮不在浏览器当前可视区域内,ActionChains.move_to_element()无法准确定位元素位置导致的;另外页面加载速度较慢时,也可能出现元素未完全渲染就执行操作的情况。

修正方案

推荐使用JavaScript直接点击按钮的方式,这种方法不受元素是否在可视区域的限制,稳定性更高;同时增加点击后的等待时间,确保新内容加载完成后再进行下一次判断。

修正后的代码

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

HOME_PAGE_URL = "https://empireflippers.com/marketplace/"

driver = webdriver.Chrome('./chromedriver.exe')
driver.get(HOME_PAGE_URL)
driver.maximize_window()  # 最大化窗口减少可视区域遮挡问题

while True:
    try:
        # 等待按钮可点击
        load_more_btn = WebDriverWait(driver, 20).until(
            EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Load More Listings')]"))
        )
        # 使用JavaScript点击按钮,规避可视区域限制
        driver.execute_script("arguments[0].click();", load_more_btn)
        # 等待新内容加载完成,可根据页面实际加载速度调整时长
        time.sleep(3)
    except TimeoutException:
        # 超时说明按钮已不存在,退出循环
        break
    except Exception as e:
        print(f"出现异常: {e}")
        break

print("所有列表加载完成")
page_source = driver.page_source
# 可选:将完整页面源码写入本地文件
# with open("full_marketplace.html", "w", encoding="utf-8") as f:
#     f.write(page_source)

driver.quit()

额外优化说明

  • 最大化浏览器窗口,减少元素被页面元素遮挡的概率
  • 精准捕获TimeoutException判断按钮是否消失,避免误捕获无关异常
  • 增加点击后的等待时间,确保新列表项加载完成,防止重复点击或提前退出循环

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 20:25:11