自动化登录12306-selenium

 1 from selenium import webdriver
 2 import time
 3 from PIL import Image
 4 from Codeclass import Chaojiying_Client
 5 from selenium.webdriver import ActionChains
 6 # 使用selenium打开登陆页面
 7 bro = webdriver.Chrome(executable_path='./chromedriver.exe')
 8 bro.get('https://kyfw.12306.cn/otn/resources/login.html')
 9 time.sleep(1)
10 bro.maximize_window()
11 time.sleep(1)
12 a_tag = bro.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a')
13 a_tag.click()
14 time.sleep(1)
15 bro.save_screenshot('aaa.png')
16 code_img_ele = bro.find_element_by_id('J-loginImg')
17 location = code_img_ele.location
18 print('location:', location)
19 
20 size = code_img_ele.size
21 print('size:', size)
22 rangle = (
23     int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height'])
24 )
25 
26 i = Image.open('aaa.png')
27 code_img_name = './code.png'
28 #crop根据指定区域进行图片裁剪
29 frame = i.crop(rangle)
30 frame.save(code_img_name)
31 
32 chaojiying = Chaojiying_Client('1294541754', 'liusi0719', '920917')  # 用户中心>>软件ID 生成一个替换 96001
33 im = open('code.png', 'rb').read()  # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
34 print(chaojiying.PostPic(im, 9004)['pic_str'])  # 1902 验证码类型  官方网站>>价格体系 3.4+版 print 后要加()
35 result = chaojiying.PostPic(im, 9004)['pic_str']
36 
37 all_list = [] #要存储即将被点击的点的坐标  [[x1,y1],[x2,y2]]
38 if '|' in result:
39     list_1 = result.split('|')
40     count_1 = len(list_1)
41     for i in range(count_1):
42         xy_list = []
43         x = int(list_1[i].split(',')[0])
44         y = int(list_1[i].split(',')[1])
45         xy_list.append(x)
46         xy_list.append(y)
47         all_list.append(xy_list)
48 else:
49     x = int(result.split(',')[0])
50     y = int(result.split(',')[1])
51     xy_list = []
52     xy_list.append(x)
53     xy_list.append(y)
54     all_list.append(xy_list)
55 print(all_list)
56 #遍历列表,使用动作链对每一个列表元素对应的x,y指定的位置进行点击操作
57 for l in all_list:
58     x = l[0]
59     y = l[1]
60     ActionChains(bro).move_to_element_with_offset(code_img_ele, x, y).click().perform()
61     time.sleep(0.5)
62 
63 bro.find_element_by_id('J-userName').send_keys('12345678')
64 time.sleep(2)
65 bro.find_element_by_id('J-password').send_keys('fsfdsfsdf')
66 time.sleep(2)
67 bro.find_element_by_id('J-login').click()
68 time.sleep(3)
69 bro.quit()

 

posted @ 2021-08-21 18:07  likl-  阅读(143)  评论(0)    收藏  举报