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:UsersrianDesktopRestartLogslog.txt","r+") as f: 11 old = f.read() 12 f.seek(0,0) 13 f.writelines(now_log) 14 f.writelines(' ') 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