selenium 破解东方航空登录滑块验证码

from selenium import webdriver
from PIL import Image
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.action_chains import ActionChains
import time
import random

def cut_image(driver):
# 获取整个页面图片,图片名字为'whole_img.png'
driver.save_screenshot('whole_img.png')
image = driver.find_element_by_class_name('geetest_canvas_img')

# 获取图片的左上右下的位置
left = image.location['x']
top = image.location['y']
right = left + image.size['width']
buttom = top + image.size['height']

# 调用open方法打开全屏图片并赋值给image_obj对象
whole_img_obj = Image.open('whole_img.png')

# 通过image_obj对象对小图片进行截取
img = whole_img_obj.crop((left, top, right, buttom))
return img

def get_image(driver, target):
time.sleep(2)
# 修改document文档树,把完整图片的display属性修改为block或 none (根据参数修改)
js_code = '''var x = document.getElementsByClassName("geetest_canvas_fullbg")[0].style.display = "{}";'''.format(target)
# 执行js代码
driver.execute_script(js_code)
# 截取图片
image = cut_image(driver)
return image

def is_similar(full_image, gap_image, x, y):
# 像素差
num = 50
pixel1 = full_image.getpixel((x, y))
pixel2 = gap_image.getpixel((x, y))

for i in range(0, 3):
if abs(pixel1[i]) - pixel2[i] >= num:
return False
return True

def get_distance(full_image, gap_image):
start = 60
for x in range(start, full_image.size[0]):
for y in range(full_image.size[1]):
if not is_similar(full_image, gap_image, x, y):
return x

def get_tracks(distance):
'''
滑动行为轨迹
加速公式: v = v0 + a * t
路程公式: s = v0 * t + 0.5 * a * (t ** 2)
'''
# 初速度
v0 = 0
# 时间
t = 0.3
# 位置
s = 0
# 滑动轨迹列表 向前滑动列表
move_list = []
# 中间值,作为加减速度的位置
mid = distance * 7 / 8
# 加减速度列表
v_list = [4, 8, 18, 28]
# 循环位移
while s < distance:
if s < mid:
# 随机获取一个加速度
a = v_list[random.randint(0, len(v_list) - 1)]
else:
# 随机获取一个减速度
a = -v_list[random.randint(0, len(v_list) - 1)]
'''
匀加速/减速运行: v = v0 + a * t
位移: s = v * t + 0.5 * a * (t**2)
'''
# 获取初始速度
v = v0
# 路程公式:
s1 = v * t + 0.5 * a * (t ** 2)
s1 = round(s1) # 取整
# 加速公式:
m_v = v + a * t
# 把当前加/减速度赋值给初始速度,以便下一次计算
v0 = m_v
# 把位移添加到滑动列表中
move_list.append(s1)
# 修改滑动初始距离
s += s1
# 后退列表, 自定义后退滑动轨迹,必须是负值
back_list = [-1, -1, -2, -3, -2, -2, -1]
return {'move_list': move_list, 'back_list': back_list}

def main():
    path = r'C:\Users\software\chromedriver\chromedriver_win32\chromedriver.exe'
options = ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(executable_path=path, options=options)
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
})
driver.maximize_window()
    try:
# 1. 输入账号,密码,登录
driver.get("https://passport.ceair.com/?redirectUrl=http%3A%2F%2Fwww.ceair.com%2F#/ffp")

driver.find_element_by_xpath('//*[@id="login"]/section/article/aside/article/dl[1]/dd/input').send_keys(
'19937423832')
time.sleep(0.2)

driver.find_element_by_xpath('//*[@id="login"]/section/article/aside/article/dl[2]/dd/input').send_keys(
'19283746')
time.sleep(0.3)

driver.find_element_by_xpath('//*[@id="login"]/section/article/aside/article/dl[4]/dd/input').click()
time.sleep(1)
        # 2.获取完整图片
full_image = get_image(driver, 'block')
# full_image.show()

# 3.获取缺口图片
gap_image = get_image(driver, 'none')
# gap_image.show()

# 4.对比两张图片,获取滑动距离
distance = get_distance(full_image, gap_image)

# 5.模拟人的滑动轨迹
tracks = get_tracks(distance)
# 获取前进滑动轨迹
move_list = tracks['move_list']
# 获取后退滑动轨迹
back_list = tracks['back_list']

# 6.滑动
move_tag = driver.find_element_by_xpath('/html/body/div/div[2]/div[6]/div/div[1]/div[2]/div[2]')
# 点击摁住滑动按钮
ActionChains(driver).click_and_hold(on_element=move_tag).perform()
for move in move_list:
ActionChains(driver).move_by_offset(xoffset=move, yoffset=0).perform()
time.sleep(0.1)

time.sleep(0.1)

# 向后滑动
for back in back_list:
ActionChains(driver).move_by_offset(xoffset=back, yoffset=0).perform()
time.sleep(0.1)

# 7.微妙晃动
ActionChains(driver).move_by_offset(xoffset=3, yoffset=0).perform()
time.sleep(0.1)
ActionChains(driver).move_by_offset(xoffset=-3, yoffset=0).perform()
time.sleep(0.1)

# 8.释放滑动按钮
ActionChains(driver).release().perform()
time.sleep(1)
    except Exception as e:
print('exception: ', e)
driver.close()
if __name__ == '__main__':
main()

猜你喜欢

转载自www.cnblogs.com/lishuaijiang/p/12789112.html