当前位置:   article > 正文

12306 模拟登录_12306模拟登录

12306模拟登录

@selenium 模拟登录

#!/usr/bin/env python
# coding:utf-8

from chaojiying import Chaojiying_Client
from selenium import webdriver
from time import sleep
from PIL import Image
from selenium.webdriver import ActionChains


url = 'https://kyfw.12306.cn/otn/resources/login.html'
bro = webdriver.Chrome(executable_path='./chromedriver')
bro.maximize_window()
bro.get(url)
btn = bro.find_element_by_link_text('账号登录')
sleep(1)
btn.click()
sleep(3)

# 获取验证码图片
# # 方法一,截取全屏,获取验证码图片的左上角、右下角坐标,裁剪图片(pillow),
# bro.save_screenshot('./aa.png')
#
# code_img_ele = bro.find_element_by_xpath('//*[@id="J-loginImg"]')
# location = code_img_ele.location # 验证码图片左上角的坐标 x,y
# size = code_img_ele.size # 验证码标签对应的长和宽
# print('location', location)
# print('size', size)
# # 左上角和右下角坐标
# rangle = (
#     int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height'])
# )
# # 至此验证码图片区域就确定下来了
#
# i = Image.open('./aa.png')
# code_img_name = './code.png'
# # crop 根据指定区域进行图片裁剪
# frame = i.crop(rangle)
# frame.save(code_img_name)

# 方法二 直接定位验证码图片标签位置截图
code_img_ele = bro.find_element_by_xpath('//*[@id="J-loginImg"]')
code_img_ele.screenshot('./code.png')


# 将验证码图片提交给超级鹰进行识别
chaojiying = Chaojiying_Client('username', 'password', '96001')	#用户中心>>软件ID 生成一个替换 96001
im = open('code.png', 'rb').read()													#本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
print(chaojiying.PostPic(im, 9004)['pic_str'])




# 下面进行页面点击
result = chaojiying.PostPic(im, 9004)['pic_str']
all_list = []  # 要存储即将被点击的点的坐标  [[x1,y1],[x2,y2]]

if '|' in result:
    list_1 = result.split('|')
    count_1 = len(list_1)
    for i in range(count_1):
        xy_list = []
        x = int(list_1[i].split(',')[0])
        y = int(list_1[i].split(',')[1])
        xy_list.append(x)
        xy_list.append(y)
        all_list.append(xy_list)
else:
    x = int(result.split(',')[0])
    y = int(result.split(',')[1])
    xy_list = []
    xy_list.append(x)
    xy_list.append(y)
    all_list.append(xy_list)
print(all_list)

# 遍历列表,使用动作链对每一个列表元素对应的x,y指定的位置进行点击操作
for l in all_list:
    x = l[0]
    y = l[1]
    # code_img_ele上面定位的验证码的位置
    ActionChains(bro).move_to_element_with_offset(code_img_ele, x, y).click().perform()
    sleep(0.5)

bro.find_element_by_id('J-userName').send_keys('username')
sleep(0.5)
bro.find_element_by_id('J-password').send_keys('password')
sleep(0.6)
bro.find_element_by_id('J-login').click()
sleep(1)


# 防止12306禁止selenium(使用selenium滑动会被12306检测到,需要伪装一下)
script = 'Object.defineProperty(navigator,"webdriver",{get:()=>undefined,});'
bro.execute_script(script)
# 滑动验证
div = bro.find_element_by_xpath('//*[@id="nc_1_n1z"]')
action = ActionChains(bro)
action.click_and_hold(div)
action.move_by_offset(350, 0).perform()
sleep(10)

action.release()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/143158?site
推荐阅读
相关标签
  

闽ICP备14008679号