ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

使用selenium模拟登录12306网站

2021-04-22 22:03:43  阅读:183  来源: 互联网

标签:code 登录 int selenium list bro 12306 location xy


 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()

 

标签:code,登录,int,selenium,list,bro,12306,location,xy
来源: https://www.cnblogs.com/haoyujirui/p/14691563.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有