from selenium import webdriver
import time
import requests
from lxml import etree
from urllib import request
from selenium.webdriver.common.action_chains import ActionChains
from PIL import Image
bro = webdriver.Chrome(executable_path='/Users/bobo/Desktop/oldBoy爬虫相关/chromedriver')
bro.get('https://kyfw.12306.cn/otn/login/init')
# page_text = bro.page_source
# tree = etree.HTML(page_text)
# code_img_src = tree.xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img/@src')[0]
# print(code_img_src)
# request.urlretrieve(url=code_img_src,filename='./code.jpg')
time.sleep(3)
code_img_ele = bro.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img')
time.sleep(3)
location = code_img_ele.location # x,y
size = code_img_ele.size #宽度
rangle = (int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height']))
bro.save_screenshot('aa.png')
i = Image.open('./aa.png')
code_img_name = 'code.png'
frame = i.crop(rangle)
frame.save(code_img_name)
chaojiying = Chaojiying_Client('bobo328410948', 'bobo328410948', '899370')#用户中心>>软件ID 生成一个替换 96001
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:
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)
code_img = bro.find_element_by_xpath('//*[@id="loginForm"]/div/ul[2]/li[4]/div/div/div[3]/img')
action = ActionChains(bro)
for l in all_list:
x = l[0]
y = l[1]
ActionChains(bro).move_to_element_with_offset(code_img,x,y).click().perform()
bro.find_element_by_id('username').send_keys('13436427103')
time.sleep(2)
bro.find_element_by_id('password').send_keys('asdf1234')
time.sleep(2)
bro.find_element_by_id('loginSub').click()
time.sleep(10)
bro.quit()