GlidedSky验证码识别采用跟腾讯防水墙的滑动验证码是一样的验证码
这里需要注意的就是它在iframe中,iframe属于内嵌页面。所以需要进入内嵌页面才能获取到它的滑动验证码的背景图片和滑块图片等信息,selenium进入内嵌页面的代码
# 因为滑动验证码面板存在另一个iframe中
# 获取iframe对象
iframe = driver.find_element_by_id('tcaptcha_iframe')
# 进入iframe对象中
driver.switch_to.frame(iframe)
验证码代码主要分为如下模块:
def run():
#初始化自动化工具
init()
#登录的步骤
login()
#获取图片并保存
get_img()
#获取滑动验证码的按钮
button = get_button()
#获取验证码轨迹
track = get_yzm()
#移动验证码
move(track_list=track,slid_ing=button)
#关闭selenium对象
print('登录成功')
close()
在内嵌页面可以找验证码图片及滑动按钮的信息
#获取按钮
def get_button():
button = driver.find_element_by_id("tcaptcha_drag_thumb")
return button
#获取图片
def get_img():
bj_url = driver.find_element_by_id('cdn1').get_attribute('src')
hk_url = driver.find_element_by_id('cdn2').get_attribute('src')
response = requests.get(bj_url)
with open('bj.jpg', 'wb') as f:
f.write(response.content)
response = requests.get(hk_url)
with open('hk.jpg', 'wb') as f:
f.write(response.content)
获取(这里是通过opencv获取的)、轨迹和验证码位置代码如下:
# 获取验证码位置
def get_yzm():
#获取页面中验证码位置
bk_block = driver.find_element_by_id("slideBg")
web_image_width = bk_block.size
web_image_width = web_image_width['width']
bk_block_x = bk_block.location['x']
print(f"当前背景位置x:{bk_block_x}")
print(f"当前背景网页位置:{web_image_width}")
#获取页面中小滑块位置
slide_block = driver.find_element_by_id('slideBlock') # 小滑块
slide_block_x = slide_block.location['x']
print(f"当前滑块X坐标:{slide_block_x}")
img_bkblock = Image.open('bj.jpg')
real_width = img_bkblock.size[0]
# print(real_width)
width_scale = float(real_width) / float(web_image_width)
position = get_position()
real_position = position[1] / width_scale
real_position = real_position - (slide_block_x - bk_block_x)
print(f'滑块距离:{real_position}')
track_list = get_track(real_position + 4)
print(f"轨迹:{track_list}")
return track_list
模块移动就是模拟人移动,按住滑块按钮不放慢慢加速拖到快到缺口出再减速拖到。
#移动滑块
def move(track_list,slid_ing):
ActionChains(driver).click_and_hold(on_element=slid_ing).perform() # 点击鼠标左键,按住不放
time.sleep(0.2)
# print('第二步,拖动元素')
for track in track_list:
ActionChains(driver).move_by_offset(xoffset=track, yoffset=0).perform() # 鼠标移动到距离当前位置(x,y)
time.sleep(0.002)
# ActionChains(driver).move_by_offset(xoffset=-random.randint(0, 1), yoffset=0).perform() # 微调,根据实际情况微调
time.sleep(0.002)
# print('第三步,释放鼠标')
ActionChains(driver).release(on_element=slid_ing).perform()
效果视频一直在审核中所以没办法安利了。
有兴趣的铁子可以一起谈论给我建议一起学习共同进步。需要完整代码的铁子可以私信喔。