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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值