Selenium多次点击仍无法通过Cloudflare人机验证求助
解决Cloudflare人机验证无法通过的问题
问题现象
点击Cloudflare验证复选框后页面回到验证状态,尝试过三种方案均无效:
- 使用带历史记录、Cookie和缓存的浏览器配置文件
- 不使用配置文件和数据
- 通过
--remote-debugging-port启动Chromium并连接Selenium
但在同一Selenium会话中手动复制链接到新标签页点击复选框可正常通过。
核心原因
Cloudflare会检测自动化工具特征(比如Selenium的window.navigator.webdriver标记、非自然的交互节奏),判定请求为自动化行为,从而拦截验证。
解决方案
1. 隐藏Selenium自动化特征
添加Chrome参数禁用自动化检测,并修改浏览器属性:
chrome_options.add_argument("--disable-blink-features=AutomationControlled") # 初始化driver后执行脚本修改webdriver属性 driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
2. 优化浏览器配置,模拟真实用户
- 修正User-Agent,确保与Chromium版本匹配(避免混用Edge的UA):
chrome_options.add_argument("user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chromium/114.0.0.0 Safari/537.36") - 添加更多模拟真实环境的参数:
chrome_options.add_argument("--start-maximized") chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--disable-dev-shm-usage") chrome_options.add_argument("--disable-extensions") chrome_options.add_argument("--disable-gpu") chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) chrome_options.add_experimental_option("useAutomationExtension", False)
3. 调整交互逻辑,贴近真实用户行为
- 用
WebDriverWait替代固定sleep,等待元素可交互后再操作:from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 示例:等待搜索框可点击 input_box = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, "search-panel")) ) - 模拟真实输入节奏,分字符输入:
address = "0x896463c65b70da9bbe267c2feb97ab59fc715506" for char in address: input_box.send_keys(char) sleep(0.1) # 小间隔模拟手动输入 - 用
ActionChains模拟滚动或点击:from selenium.webdriver.common.action_chains import ActionChains # 模拟滚动到元素位置 wallet_icon = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, "//i[@class='far fa-wallet me-1']")) ) ActionChains(driver).move_to_element(wallet_icon).perform() wallet_icon.click()
4. 使用undetected-chromedriver(推荐)
这个库专门针对Cloudflare检测做了优化,绕过效果更好:
from os import path from time import sleep from undetected_chromedriver import Chrome, ChromeOptions 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 = ChromeOptions() chrome_options.add_argument("profile-directory=Default") chrome_options.add_argument("user-data-dir=/home/abhishek/.config/chromium/") chrome_options.binary_location = "/usr/bin/chromium" try: driver = Chrome(options=chrome_options, executable_path=path.join("/home", "abhishek", "opt", "chromedriver_linux", "chromedriver")) driver.get("https://etherscan.io") # 等待搜索框并操作 input_box = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, "search-panel")) ) input_box.click() address = "0x896463c65b70da9bbe267c2feb97ab59fc715506" for char in address: input_box.send_keys(char) sleep(0.1) # 点击搜索结果 WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.CSS_SELECTOR, "div.flex-fill.text-truncate")) ).click() # 点击余额下拉菜单 WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, "dropdownMenuBalance")) ).click() # 滚动到钱包图标并点击 wallet_icon = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, "//i[@class='far fa-wallet me-1']")) ) driver.execute_script("arguments[0].scrollIntoView();", wallet_icon) wallet_icon.click() sleep(10) except Exception as e: print("OUT", e) if 'driver' in locals(): driver.close()
内容的提问来源于stack exchange,提问作者ABHISHEK TIWARI
相关产品推荐
相关产品推荐

