码迷,mamicode.com
首页 > Web开发 > 详细

使用selenium模拟登录12306网站

时间:2021-04-23 12:10:24      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:存储   切换   动作   element   登录   最大化   tab   elements   rgb   

 1 import yh
 2 from selenium import webdriver
 3 from PIL import Image
 4 # from selenium.webdriver import ActionChains
 5 # from selenium.webdriver import ActionChains
 6 import time
 7 from selenium.webdriver import ActionChains
 8 bro = webdriver.Chrome(executable_path=./chromedriver)
 9 bro.get(https://kyfw.12306.cn/otn/resources/login.html)
10 time.sleep(1)
11 #登录界面后点击账号密码登录
12 #查找标签
13 bro.maximize_window()   #将浏览器最大化
14 # login = bro.find_elements_by_xpath(‘/html/body/div[2]/div[2]/ul/li[2]/a‘)
15 login = bro.find_element_by_xpath(/html/body/div[2]/div[2]/ul/li[2])
16 login.click()
17 
18 #save_screenshot对当前页面进行截图
19 bro.save_screenshot(./aaa.png)
20 
21 #先找到验证码图片对应的标签
22 code_img_ele = bro.find_element_by_xpath(//*[@id="J-loginImg"])
23 #确定验证码图片对应的左上角和右下角的坐标(裁剪的区域就确定了)
24 
25 location = code_img_ele.location  #验证码图片左上角的坐标  x y
26 print(location:,location)
27 size = code_img_ele.size          #验证码对应的长和宽
28 print(size:,size)
29 
30 #左上角和右下角的坐标
31 rangle = (
32 int(location[x]), int(location[y]), int(location[x] + size[width]), int(location[y] + size[height]))
33 #至此验证码图片区域就确定下来了
34 
35 time.sleep(3)
36 i = Image.open(./aaa.png)
37 code_img_name = ./code.png
38 
39 #crop根据指定区域进行图片裁剪
40 frame = i.crop(rangle)
41 frame.save(code_img_name)
42 
43 #使用超级鹰进行坐标识别
44 result = yh.get_code(code.png,9004)
45 print(result)
46 
47 all_list = []  #要存储即将被点击的点的坐标.   [[x1,y1],[x2,y2],[x3,y3]]
48 if | in result:   #有多个坐标的情况下进行保存
49     list_1 = result.split(|)   #把返回的所有左边按照|进行分割
50     content_1 = len(list_1)      #总共有多少个坐标的数量
51     for i in range(content_1):
52         xy_list = []
53         x = int(list_1[i].split(,)[0])   #获取第i个坐标的x值
54         y = int(list_1[i].split(,)[1])   #获取第i个坐标的y值
55         xy_list.append(x)
56         xy_list.append(y)
57         all_list.append(xy_list)
58 
59 else:   #有一个坐标的情况下进行保存
60     x = int(result.split(,)[0])
61     y = int(result.split(,)[1])
62     xy_list = []
63     xy_list.append(x)
64     xy_list.append(y)
65     all_list.append(xy_list)
66 print(all_list)
67 
68 #遍历列表,使用动作链对每一个列表元素对应的x y 指定的位置进行点击操作
69 for l in all_list:
70     x = l[0]
71     y = l[1]
72     #实例化动作链对象,切换验证码页面,立即进行点击
73     ActionChains(bro).move_to_element_with_offset(code_img_ele,x,y).click().perform()
74 
75 bro.find_element_by_id(J-userName).send_keys(15617567868)
76 time.sleep(2)
77 bro.find_element_by_id(J-password).send_keys(WCH19920816)
78 time.sleep(2)
79 bro.find_element_by_id(J-login).click()
80 time.sleep(3)
81 bro.quit()

 

使用selenium模拟登录12306网站

标签:存储   切换   动作   element   登录   最大化   tab   elements   rgb   

原文地址:https://www.cnblogs.com/haoyujirui/p/14691563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!