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

求助:Selenium Python无法批量点击页面所有‘more’按钮的问题

批量点击页面中“more”按钮的问题解决

我需要批量点击页面上所有文本为“more”的按钮,但尝试了三种方法都失败了,页面地址为https://religiondatabase.org/browse/regions,具体尝试情况如下:

尝试方案1:ActionChains点击

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

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

chrome_options = Options()
#chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(chrome_options=chrome_options)
driver.maximize_window()

driver.get('https://religiondatabase.org/browse/regions')

# Wait for the page to load
wait = WebDriverWait(driver, 10)

# Find all buttons containing the text "more"
buttons = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//button[contains(text(), 'more')]")))
print(buttons)
actions = ActionChains(driver)

for button in buttons:
    actions.click(button).perform()

错误信息:

Message: move target out of bounds

尝试方案2:坐标滚动后点击

for button in buttons:
    coordinates = button.location_once_scrolled_into_view # returns dict of X, Y coordinates
    driver.execute_script('window.scrollTo({}, {});'.format(coordinates['x'], coordinates['y']))
    button.click()

错误信息:

Element is not clickable at point(x,y)

尝试方案3:JavaScript循环点击

for i in range(len(buttons)):
    buttons = driver.find_elements(By.XPATH, "//button[contains(text(), 'more')]")
    button = buttons[i]
    driver.execute_script("arguments[0].click();", button)

错误信息:

Message: stale element reference: element is not attached to the page document


可行解决方案

问题核心在于点击“more”按钮后页面DOM会重新渲染,导致之前定位的元素失效,同时部分按钮可能被页面元素遮挡或未完全加载。以下是调整后的代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chrome_options = Options()
# chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(options=chrome_options)
driver.maximize_window()
driver.get('https://religiondatabase.org/browse/regions')
wait = WebDriverWait(driver, 10)

while True:
    # 每次循环重新定位所有"more"按钮,避免stale元素问题
    try:
        # 等待第一个可点击的"more"按钮出现
        button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'more')]")))
        # 滚动到按钮居中位置,确保不被遮挡
        driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", button)
        # 使用JS点击,规避元素遮挡问题
        driver.execute_script("arguments[0].click();", button)
        # 等待页面渲染完成,避免后续定位出错
        wait.until(EC.staleness_of(button))
    except:
        # 没有更多"more"按钮时退出循环
        break

driver.quit()

关键调整点:

  • 用while True循环,每次重新定位按钮,解决DOM更新导致的元素失效问题
  • 等待可点击的按钮而非仅存在的元素,确保按钮处于交互状态
  • 滚动到按钮居中位置,避免被导航栏或底部元素遮挡
  • 点击后等待原按钮失效,确保页面渲染完成后再进行下一次定位
  • 捕获异常,无更多按钮时自动退出循环

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 18:42:32