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

使用Selenium操作Rarible网站无法点击Follow按钮求助

Rarible平台Selenium自动化Follow按钮无法点击解决方案

核心问题排查

代码中的问题主要出在定位逻辑、等待机制和点击处理三个环节,对应修复点如下:

  • 绝对路径XPATH稳定性极差,前端动态渲染、页面结构稍有调整就会定位错误,你注释中写的相对定位//button[normalize-space()="Follow"]可靠性远高于全路径XPATH
  • 硬等待(sleep)无法适配不同网络环境下的元素加载节奏,经常出现元素未完全渲染就执行点击的情况,要替换为显式等待
  • 未处理元素不在可视区域、前端自定义事件拦截的场景,原生Selenium点击经常失效,可改用JS执行点击
  • 递归遍历逻辑没有终止条件,也没有异常捕获,遍历到粉丝列表末尾、或者遇到未加载出来的元素时会直接崩溃

修复后代码

from selenium import webdriver
from time import sleep
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 = webdriver.ChromeOptions()
# 增加反检测配置,避免平台识别自动化工具拦截操作
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option("useAutomationExtension", False)
driver = webdriver.Chrome(r'chromedriver.exe', options=chrome_options)
# 移除webdriver特征标识
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")

url = 'https://rarible.com/connect'

def main():
    driver.get(url)
    input('Login..(Press ENTER when finished)')
    sleep(1)
    while driver.current_url == "https://rarible.com/":
        # 后续建议也替换为带属性的相对定位,避免页面结构变动失效
        ts_href = '/html/body/div[1]/div[2]/div[2]/div[2]/div/div/div/div[4]/div[2]/div/div[1]/div[1]/div/div/div[' \
                  '1]/div[1]/div/div[3]/a'
        href = driver.find_element(By.XPATH, ts_href).get_attribute('href')
        driver.get(href)
        sleep(2)
        # 粉丝按钮点击增加显式等待,避免未加载完成就点击
        followers_xpath = '/html/body/div/div[2]/div[2]/div[2]/div[2]/div/div/div/div[1]/div[5]/div/button[1]/span[2]'
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, followers_xpath))).click()
        sleep(3)
        b = 1 # 每次进入粉丝页重置计数,避免全局变量数值残留
        
        def butts():
            nonlocal b
            try:
                fbtn_xpath = f'/html/body/div/div[1]/div/div[2]/div/div[2]/div/div/div/div[{b}]/div/div/div[3]/button'
                # 显式等待按钮加载完成
                button = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, fbtn_xpath)))
                # 滚动到按钮可视区域,避免元素不在视口导致点击失败
                driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", button)
                sleep(0.5)
                btn_text = button.text.strip()
                print(f'BUTTON {b} TEXT (FOLLOW/UNFOLLOW): {btn_text}')
                if btn_text == "Follow":
                    # 用JS点击规避前端自定义事件拦截
                    driver.execute_script("arguments[0].click();", button)
                    b += 1
                    sleep(1)
                    butts()
                elif btn_text == "Unfollow":
                    b += 1
                    butts()
            except:
                # 捕获异常说明已经遍历完所有粉丝,终止递归
                print(f"已遍历到第{b}个元素,无更多可操作按钮,终止遍历")
                return

        butts()
        print('All set here, onto the next one...')
    else:
        driver.get('https://rarible.com/')

if __name__ == '__main__':
    main()

额外优化建议

  • 后续可以把所有绝对路径XPATH都替换为带属性的相对定位,比如通过class、data-testid等属性定位,稳定性会更高
  • 可以取消递归逻辑,直接用find_elements批量获取所有Follow按钮之后遍历操作,代码可读性和稳定性都会更好

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 00:45:04