首页 > 代码库 > python抢小米6自动化脚本

python抢小米6自动化脚本

 1 # -*- coding:utf-8 -*-
 2 
 3 from selenium import webdriver
 4 from selenium.webdriver.common.by import By
 5 from selenium.webdriver.common.keys import Keys
 6 from selenium.webdriver.support.ui import Select
 7 from selenium.common.exceptions import NoSuchElementException
 8 from selenium.common.exceptions import NoAlertPresentException
 9 import unittest
10 import time
11 import re
12 import httplib
13 
14 
15 class XiaoMi(unittest.TestCase):
16     def setUp(self):
17         self.driver = webdriver.Chrome()
18         self.base_url = http://item.mi.com/
19         self.verificationErrors = []
20         self.accept_next_alert = True
21 
22     def login():
23         """
24         用户登陆
25         """
26         driver = webdriver.Chrome()
27         driver.get("http://item.mi.com/product/10000041.html")
28         driver.find_element_by_xpath(".//*[@id=‘J_userInfo‘]/a[1]").click()
29         time.sleep(2)
30         driver.find_element_by_xpath(".//*[@id=‘username‘]").clear()
31         driver.find_element_by_xpath(".//*[@id=‘username‘]").send_keys(用户名)
32         driver.find_element_by_xpath(".//*[@id=‘pwd‘]‘]").clear()
33         driver.find_element_by_xpath(".//*[@id=‘pwd‘]‘]").send_keys(密码)
34         dirver.find_element_by_id(login-button).click()
35         assertEqual(u登陆失败, u1253785032, driver.find_element_by_xpath(".//*[@id=‘J_userInfo‘]/span[1]/a/span").span)
36         
37 
38     def get_xiaomi6(self):
39         ‘‘‘
40         设置9:59:55开始浏览器模拟用户行为不停的点击加入购物车
41         ‘‘‘
42         def getSysTime():
43             sys_time = time.time()
44             return sys_time
45 
46         def set_stamp():
47             set_time = 2017-05-12 09:59:55   #设置抢购时间,最好提前几秒
48             # 将其转换为时间数组
49             timeArray = time.strptime(set_time, %Y-%m-%d %H:%M:%S)
50             # 转换为时间戳
51             timeStamp = int(time.mktime(timeArray))
52             return timeStamp
53 
54         if getSysTime() >= set_stamp():
55             while True:
56                 self.driver.find_element_by_class_name(btn btn-primary btn-biglarge J_proBuyBtn add).click()
57 
58                 if self.assertTrue(u你来晚了, driver.title) == True:
59                     print 又悲剧了,默默的问候小米~
60                     break
61 
62     def is_element_present(self):
63         try:self.driver.find_element(by=how, value=http://www.mamicode.com/what)
64         except NoSuchElementException as e:return False
65         return True
66 
67     def is_alert_present(self):
68         try:self.driver.switch_to_alert()
69         except NoAlertPresentException as e:return False
70         return True
71 
72     def close_alert_and_get_its_text(self):
73         try:
74             alert = self.driver.switch_to_alert()
75             alert_text = alert.text
76             if self.accept_next_alert:
77                 alert.accept()
78             else:
79                 alert.dismiss()
80             return alert_text
81         finally:
82             self.accept_next_alert = True
83 
84     def tearDown(self):
85         pass
86         #self.driver.quit()
87         #self.assertEqual([], self.verificationErrors)
88 
89 if __name__ == __main__:
90     unittest.main()

 

python抢小米6自动化脚本