from util import chaojiying_util as cu
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from PIL import Image
def calxy(result_list):
x_list = []
y_list = []
if '|' in result_list:
list_list = result_list.split('|')
for i in range(len(list_list)):
split_list(list_list[i], x_list, y_list)
else:
split_list(result_list, x_list, y_list)
return x_list, y_list
def split_list(result_list, x_list, y_list):
in_list = result_list.split(',')
x_list.append(int(in_list[0]))
y_list.append(int(in_list[1]))
return x_list, y_list
url = 'https://kyfw.12306.cn/otn/resources/login.html'
bro = webdriver.Chrome(executable_path='chromedriver.exe')
bro.get(url=url)
bro.maximize_window()
time.sleep(2)
a_tag = bro.find_element_by_class_name('login-hd-account')
a_tag.click()
time.sleep(2)
bro.save_screenshot('windows.png')
loginImg = bro.find_element_by_xpath('//*[@id="J-loginImgArea"]')
location = loginImg.location
size = loginImg.size
rangle = (
location['x'],
location['y'],
location['x'] + size['width'],
location['y'] + size['height']
)
print('验证码图片坐标',rangle)
img = Image.open('./windows.png')
code_img_name = 'code.png'
frame = img.crop(rangle)
frame.save(code_img_name)
result_list = cu.post_pic('./code.png', 9004)['pic_str']
x_list, y_list = cu.calxy(result_list)
print(all_list)
for x, y in zip(x_list, y_list):
print(x, y)
ActionChains(bro).move_to_element_with_offset(loginImg, x, y).click().perform()
bro.find_element_by_id('J-userName').send_keys('用户名')
time.sleep(2)
bro.find_element_by_id('J-password').send_keys('密码')
time.sleep(2)
bro.find_element_by_id('J-login').click()
time.sleep(2)