#使用selenium打开登录页面
from selenium import webdriver
import time
from PIL import Image
from selenium.webdriver import ActionChains
bro = webdriver.Chrome(executable_path='./chromedriver)
bro.get('https://kyfw.12306.cn/otn/login/init')
time.sleep(1)
#save_screenshot就是将当前页面进行截图保存
bro.save_screenshot('aa.png')
#确定验证码图片对应的左上角和右下角坐标以便对上一个截图裁剪
code_img_ele = bro.find_element_by_xpath('验证码图片对应复制xpath')
location = code_img_ele.location #验证码图片左上角的坐标x,y
print('location:', location)
size = code_img_ele.size #验证码标签的对应长和高,width和height
print('size:', size)
rangle = (
location['x'], location['y'], location['x']+size['width'], location['y']+size['height']
)
#裁剪区域定下来,下面是裁剪操作
i = Image.open('./aa.png')
code_img_name = 'code.png'
#crop根据指定区域进行裁剪
frame = i.crop(rangle)
frame.save(code_img_name)
#将验证码图片提交给超级鹰识别
chaojiying = Chaojiying_Client('cjyusename', 'cjypassword', '提供给对应账号的id')
im = open(code.png', 'rb').read()
result = chaojiying.PostPic(im, 9004)['pic_str']
all_list = []
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:
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)
#遍历列表,使用动作链对每一个列表元素对应的x,y指定位置进行点击操作
for l in all_list:
x = l[0]
y = l[1]
ActionChains(bro).move_to_element_with_offset(code_img_ele,x,y).click().perform()
time.sleep(0.5)
bro.find_element_by_id('username'.send_keys('xxxx')
time.sleep(0.5)
bro.find_element_by_id('password'.send_keys('xxxx')
time.sleep(0.5)
bro.find_element_by_id('LoginSub'.click()
time.sleep(0.5)
bro.quit()