欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

过年回家抢票,让光猫自动重启的小脚本

程序员文章站 2022-11-22 07:51:27
1 from selenium import webdriver 2 from time import sleep 3 import datetime 4 url = "http://192.168.1.1/login_smart.html" 5 url_login = "http://192.16... ......
 1 from selenium import webdriver
 2 from time import sleep
 3 import datetime
 4 url = "http://192.168.1.1/login_smart.html"
 5 url_login = "http://192.168.1.1/login.cgi"
 6 url_longout = "http://192.168.1.1/login.cgi#"
 7 rstart_time = datetime.datetime.now().strftime('%y-%m-%d %h:%m:%s')
 8 i = 0
 9 def write_record(now_log):
10     with open(r"c:\users\brian\desktop\restartlogs\log.txt","r+") as f:
11         old = f.read()
12         f.seek(0,0)
13         f.writelines(now_log)
14         f.writelines('\n')
15         f.writelines(old)
16         f.close()
17 
18 while true:
19     try:
20         dr = webdriver.chrome()
21         dr.get(url)
22         dr.maximize_window()
23         sleep(0.1)
24         dr.find_element_by_css_selector("#loid_user").clear()
25         sleep(0.1)
26         dr.find_element_by_css_selector("#loid_user").send_keys("useradmin")
27         sleep(0.1)
28         dr.find_element_by_css_selector("#loid_pwd").send_keys("a124689a")
29         sleep(0.1)
30         dr.find_element_by_css_selector("#save_loid").click()
31         sleep(10)
32         if dr.current_url == url_login:
33             print("登录成功")
34         dr.find_element_by_css_selector("[alt='重启']").click()
35         sleep(0.1)
36         dr.find_element_by_css_selector("#okbutton1").click()
37         if dr.current_url == url_longout:
38             new_log = "重启成功,重启时间是:%s" %rstart_time
39             print(new_log)
40             write_record(new_log)
41             dr.quit()
42             break
43         else:
44             dr.quit()
45             print("重启失败了,关闭浏览器再来")
46             continue
47     except:
48         dr.quit()
49         print("代码运行出错,关闭浏览器再来")
50         i = i +1
51         if i >= 3:
52             break
53         else:
54             continue