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

使用Selenium上传Redbubble时Cloudflare验证绕过失败求助

问题:Redbubble上传图片时无法绕过Cloudflare验证

我在向Redbubble上传图片时必须绕过Cloudflare验证,但多次尝试都失败了。我的需求是:当Cloudflare验证界面出现时,自动点击复选框,直到完成要求的点击次数(比如25次),直到目标元素出现。更新代码后仍无法点击复选框,当前报错如下:

Traceback (most recent call last):
  File "C:\Users\yazha\AppData\Roaming\JetBrains\PyCharmCE2023.1\scratches\scratch_6.py", line 14, in <module>
    cf_element = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "label.ctp-checkbox-label")))
  File "D:\Python files\undetected-chromedriver-master\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 95, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

Cloudflare验证界面相关截图

  • Cloudflare验证界面:Cloudflare验证界面
  • Cloudflare验证元素:Cloudflare验证元素
  • 后续需定位的目标元素:目标元素

我的代码尝试

from selenium import webdriver
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

# 初始化驱动
driver = webdriver.Chrome()
driver.maximize_window()
# 打开Redbubble网站并点击登录
driver.get('redbubble url')
wait = WebDriverWait(driver, 20)
cf_element = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "label.ctp-checkbox-label")))
num_attempts = 0
while True:
    try:
        cf_element.click()
        num_attempts += 1
        wait.until(EC.invisibility_of_element(cf_element))
        cf_element = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "select-image-single")))
    except Exception as e:
        print(f"Cloudflare captcha bypassed {num_attempts} times.")
        break
# 绕过验证码后的后续操作
driver.get(the redbubble url)
time.sleep(20)
driver.find_element(By.ID, "select-image-single").click()

解决建议

  1. 修正元素定位方式:By.CLASS_NAME仅支持单个类名,不能传入label.ctp-checkbox-label这种CSS选择器格式,需改用By.CSS_SELECTOR定位验证复选框:
    cf_element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label.ctp-checkbox-label")))
    
  2. 规避Cloudflare驱动检测:普通ChromeDriver容易被Cloudflare识别,建议改用undetected-chromedriver,它能绕过大部分反爬检测。先安装依赖:
    pip install undetected-chromedriver
    
    驱动初始化代码替换为:
    import undetected_chromedriver as uc
    driver = uc.Chrome()
    
  3. 调整循环逻辑:原代码点击一次后就尝试定位目标元素,不符合“多次点击直到验证完成”的需求,重新编写循环逻辑:
    num_attempts = 0
    wait = WebDriverWait(driver, 10)
    while True:
        try:
            # 等待复选框可点击并执行点击
            cf_checkbox = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label.ctp-checkbox-label")))
            cf_checkbox.click()
            num_attempts += 1
            time.sleep(1.5)  # 给验证加载留缓冲时间
            # 检查是否已进入目标页面(判断目标元素是否存在)
            wait.until(EC.presence_of_element_located((By.ID, "select-image-single")))
            print(f"完成{num_attempts}次验证,已进入目标页面")
            break
        except:
            # 复选框未出现,说明验证已完成或页面跳转
            print(f"验证结束,共点击{num_attempts}次")
            break
    
  4. 移除重复页面跳转:代码末尾的driver.get(the redbubble url)会重新加载页面,导致之前的验证失效,直接删除该行,基于当前已加载页面操作即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 06:30:02