使用2captcha服务遇属性错误:'TwoCaptcha'对象无'normal'属性
解决
TwoCaptcha' object has no attribute 'normal'错误的办法 核心问题原因
新版本的2captcha-python库已移除了旧的normal()方法,改用image()方法处理普通图片验证码,这是导致属性不存在错误的直接原因。
具体修复步骤
- 替换过时方法:将代码中的
solver.normal('captcha/captcha.png')替换为solver.image('captcha/captcha.png') - 补充缺失导入:代码用到了
time和os模块但未导入,需在开头添加对应导入语句 - 修复路径转义:Windows系统下ChromeDriver路径的反斜杠需用原始字符串或双反斜杠转义,避免解析错误
修改后的完整代码
import time import os from selenium import webdriver from selenium.webdriver.common.by import By from twocaptcha import TwoCaptcha # 修复ChromeDriver路径的转义问题 driver = webdriver.Chrome(r"C:\Program Files (x86)\chromedriver.exe") driver.get('https://www.amazon.com') time.sleep(2) # 定位并截取验证码元素 captcha_element = driver.find_element(By.XPATH, "//img[@class='_2hXzbgz7SSP0DXCyvKWcha']") captcha_element.screenshot('captcha/captcha.png') time.sleep(2) # 配置2Captcha求解器 api_key = os.getenv('APIKEY_2CAPTCHA', '-------') solver = TwoCaptcha(api_key) try: # 使用image方法替代旧的normal方法 result = solver.image('captcha/captcha.png') except Exception as e: print("2Captcha API异常:", e) else: print(result)
内容的提问来源于stack exchange,提问作者Amen Aziz
相关产品推荐
相关产品推荐

