Selenium是ThoughtWorks公司的一个强大的开源Web功能测试工具系列,采用Javascript来管理整个测试过程,包括读入测试套件、执行测试和记录测试结果。它采用Javascript单元测试工具JSUnit为核心,模拟真实用户操作,包括浏览页面、点击链接、输入文字、提交表单、触发鼠标事件等等,并且能够对页面结果进行种种验证。也就是说,只要在测试用例中把预期的用户行为与结果都描述出来,我们就得到了一个可以自动化运行的功能测试套件。(Selenium的核心是Javascript写的,他和浏览器进行通信,把测试用例的信息发送给浏览器执行,从而达到自动化测试的目的。)
# encoding: utf-8from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Byimport timeclass 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 print("抢票%s次" % num) 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实现自动抢票功能实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对VEVB武林网网站的支持!