如何用Python Selenium绕过eBay分类网站拼图滑块验证码?
eBay Kleinanzeigen 自动化滑块验证码及反爬问题求解
我是Python和Selenium新手,想实现eBay Kleinanzeigen的自动化操作,但卡在了拼图滑块验证码这一步,目标页面是:https://www.kleinanzeigen.de/s-anzeige:gesuche/heimarbeit/k0。同时,即使手动解决验证码,后续也会因可疑行为被封禁,换VPN只能临时解决,无法根治。我已经在代码里用了窗口最大化、显式等待和随机sleep模拟人类操作,以下是我的代码片段:
class Ebay(webdriver.Chrome): #verwendeter Browser spielt auch eine Rolle, funktioniert mit Firefox bspw nicht (mehr) def __init__(self, teardown=False): self.teardown = teardown super(Ebay, self).__init__() self.implicitly_wait(20) self.maximize_window() def __exit__(self, exc_type, exc_val, exc_tb): if self.teardown: self.quit() def get_cookies_values(self, file): #see [1] with open(file, encoding='utf-8-sig') as f: dict_reader = DictReader(f) list_of_dicts = list(dict_reader) return list_of_dicts def cookies_adding(self): #see [1] cookies = get_cookies_values("ebay_cookies.csv") for i in cookies: self.add_cookie(i) self.refresh() def land_first_page(self): self.get(const.base_url) def login(self): self.implicitly_wait(15) # create wait object which wait for 15s based on the condition wait = WebDriverWait(self, 8) # Below line will click on the alle akzeptieren button wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Alle akzeptieren']"))).click() self.implicitly_wait(15) # Below line will click on Einloggen button wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Einloggen']"))).click() # Below line will send email into text box wait.until(EC.element_to_be_clickable((By.ID, "email"))).send_keys(const.email) self.implicitly_wait(30) time.sleep(random.randint(2,15)) #Below line will send password into text box wait.until(EC.element_to_be_clickable(By.CLASS_NAME, "relative inline-flex w-full after:hidden after:hidden")).click password_button = self.find_element(By.CLASS_NAME, "relative inline-flex w-full after:hidden after:hidden") time.sleep(random.randint(2,15)) #Below line will click on Einloggen button wait.until(EC.element_to_be_clickable(By.CLASS_NAME, "u-shadow-border-transition box-border inline-flex items-center justify-center gap-md whitespace-nowrap px-lg text-body-1 font-bold focus-visible:outline-none focus-visible:u-ring [&:not(:focus-visible)]:ring-inset min-w-sz-44 h-sz-44 rounded-lg bg-main text-on-main hover:bg-main-hovered enabled:active:bg-main-pressed focus-visible:bg-main-focused w-full")).click
我尝试过以下方法,但均无效:
- 保存Firefox登录状态的Cookies并导入代码
- 使用GitHub上的免费验证码求解工具
- 采用undetected-chromedriver
可行解决方案建议
强化浏览器指纹伪装
- 除了undetected-chromedriver,额外配置浏览器参数:添加
--disable-blink-features=AutomationControlled禁用自动化特征检测,从真实浏览器UA池中随机选取User-Agent,启用WebGL、Canvas指纹随机化;还可以指定日常使用的Chrome用户数据目录,通过user-data-dir参数加载,复用真实用户的浏览环境,让Cookies、缓存更贴近真实行为。
- 除了undetected-chromedriver,额外配置浏览器参数:添加
滑块验证码的轨迹模拟
- 放弃免费验证码工具,用
ActionChains模拟人类滑动轨迹:先加速拖动,再减速,加入微小停顿和偏移,避免匀速直线移动。示例代码:from selenium.webdriver.common.action_chains import ActionChains import random import time # 定位滑块元素(需替换为实际选择器) slider = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "your-slider-class"))) target_distance = 300 # 根据实际缺口距离调整 track = [] current_pos = 0 # 生成模拟滑动轨迹 while current_pos < target_distance: step = random.randint(3, 12) current_pos += step track.append(step) # 执行滑动操作 ActionChains(self).click_and_hold(slider).perform() for x in track: ActionChains(self).move_by_offset(xoffset=x, yoffset=0).perform() time.sleep(random.uniform(0.01, 0.06)) time.sleep(random.uniform(0.2, 0.4)) ActionChains(self).release().perform() - 若需识别缺口,用OpenCV对比滑块背景图和缺口图,计算准确移动距离后再执行轨迹滑动。
- 放弃免费验证码工具,用
更真实的行为模拟
- 不要只加随机sleep,模拟鼠标曲线移动:点击元素前,让鼠标从当前位置经过随机路径移动到目标元素,而非直接跳转;
- 增加随机交互:偶尔滚动页面、点击无关元素、随机停留,避免固定流程;
- 控制请求频率,操作间隔随机设置为10-30秒,避免短时间内重复请求。
账号与环境隔离
- 每个账号对应独立的浏览器用户数据目录,不要共用环境操作多账号;
- 使用静态住宅IP代理,避免频繁切换IP,确保IP地理位置和网络类型符合真实用户;
- 使用注册后有过真实使用记录的老账号,新账号更容易触发反爬。
优先考虑官方API
- 查看eBay Kleinanzeigen是否提供官方API,通过API操作是最安全的方式,完全规避反爬检测。
内容的提问来源于stack exchange,提问作者Natascha Fritschle
相关产品推荐
相关产品推荐

