简介
什么是Selenium?
Selenium是ThoughtWorks公司的一个强大的开源Web功能测试工具系列,采用Javascript来管理整个测试过程,包括读入测试套件、执行测试和记录测试结果。它采用Javascript单元测试工具JSUnit为核心,模拟真实用户操作,包括浏览页面、点击链接、输入文字、提交表单、触发鼠标事件等等,并且能够对页面结果进行种种验证。也就是说,只要在测试用例中把预期的用户行为与结果都描述出来,我们就得到了一个可以自动化运行的功能测试套件。(Selenium的核心是Javascript写的,他和浏览器进行通信,把测试用例的信息发送给浏览器执行,从而达到自动化测试的目的。)
使用说明
程序运行开始,需要输入出发地,目的地,出发时间,乘客信息,车次;乘客信息和车次可以输入多个
刚刚开始学习爬虫,selenium仅仅是解放了双手,运行效率不是很高;
程序运行时会打开chrome浏览器,因为使用的是chrome的浏览器驱动;
相关文档
https://selenium-python.readthedocs.io/installation.html#introduction
代码示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# encoding: utf-8 from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By import time class Qiangpiao( object ): def __init__( self ): driver_path = "E:\py_pachong\chromedriver.exe" # 浏览器驱动 self .drive = webdriver.Chrome(executable_path = driver_path) # 登陆链接 self .login_url = "https://kyfw.12306.cn/otn/login/init" # 登陆后的链接 self .initmy_url = "https://kyfw.12306.cn/otn/index/initMy12306" # 查询页面 self .search_url = "https://kyfw.12306.cn/otn/leftTicket/init" # 乘车人页面 self .passenger_url = "https://kyfw.12306.cn/otn/confirmPassenger/initDc" def wait_input( self ): """ 主要用于页面审核; 当输入出发地,目的地,乘车时间后,会自动出发查询submit :return: """ self .from_station = input ( "出发地:" ) self .to_station = input ( "目的地:" ) self .depart_time = input ( "出发时间:" ) self .passengers = input ( "乘客姓名:" ).split( "," ) self .trains = input ( "车次:" ).split( "," ) def _login( self ): """ 登陆验证,登陆成功后会跳转到 self.initmy_url :return: """ self .drive.get( self .login_url) WebDriverWait( self .drive, 1000 ).until( EC.url_to_be( self .initmy_url) ) print ( '登录成功' ) def _order_ticket( self ): """ 只实现了在无票的情况下去刷票 最关键的是等待验证 WebDriverWait :return: """ self .drive.get( self .search_url) WebDriverWait( self .drive, 1000 ).until( EC.text_to_be_present_in_element_value((By. ID , "fromStationText" ), self .from_station) ) WebDriverWait( self .drive, 1000 ).until( EC.text_to_be_present_in_element_value((By. ID , "toStationText" ), self .to_station) ) WebDriverWait( self .drive, 1000 ).until( EC.text_to_be_present_in_element_value((By. ID , "train_date" ), self .depart_time) ) WebDriverWait( self .drive, 10000 ).until( EC.element_to_be_clickable((By. ID , "query_ticket" )) ) searchBtn = self .drive.find_element_by_id( "query_ticket" ) searchBtn.click() WebDriverWait( self .drive, 1000 ).until( EC.presence_of_element_located((By.XPATH, ".//tbody[@id='queryLeftTable']/tr" )) ) # find_elements_by_xpath 返回的是一个列表 # find_element_by_xpath 返回的是一个元素 tr_list = self .drive.find_elements_by_xpath( ".//tbody[@id='queryLeftTable']/tr[not(@datatran)]" ) for tr in tr_list: train_num = tr.find_element_by_class_name( "number" ).text # print(train_num) if train_num in self .trains: left_ticket_td = tr.find_element_by_xpath( ".//td[4]" ).text num = 1 while left_ticket_td = = "无" : print ( "暂时无票,正在刷新" ) time.sleep( 2 ) searchBtn.click() num + = 1 if left_ticket_td ! = "无" : print (train_num + "有票" ) oderBtn = tr.find_element_by_xpath( ".//td[13]/a" ) oderBtn.click() WebDriverWait( self .drive, 1000 ).until( EC.url_to_be( self .passenger_url) ) WebDriverWait( self .drive, 1000 ).until( EC.presence_of_element_located((By.XPATH, ".//ul[@id='normal_passenger_id']/li" )) ) passenger_labels = self .drive.find_elements_by_xpath( ".//ul[@id='normal_passenger_id']/li/label" ) for passenger_label in passenger_labels: name = passenger_label.text if name in self .passengers: passenger_label.click() submitBtn = self .drive.find_element_by_id( "submitOrder_id" ) submitBtn.click() WebDriverWait( self .drive, 1000 ).until( EC.presence_of_element_located((By.CLASS_NAME, "dhtmlx_wins_body_outer" )) ) WebDriverWait( self .drive, 1000 ).until( EC.presence_of_element_located((By. ID , "qr_submit_id" )) ) qr_submit = self .drive.find_element_by_id( "qr_submit_id" ) qr_submit.click() def run( self ): self .wait_input() self ._login() self ._order_ticket() if __name__ = = '__main__' : spider = Qiangpiao() spider.run() |
总结
以上所述是小编给大家介绍的python+selenium实现自动抢票功能实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://www.cnblogs.com/mumengyun/p/10001109.html