python3實作自動重啟路由器的上的花生殼(selenium)
需求介紹:我路由器上系結的花生殼總是失效,解決辦法是重啟動一下就正常了,所以使用python3的selenium來實作,(停用啟用即可)

下載google插件如下:
http://npm.taobao.org/mirrors/chromedriver/
google插件xpath可自行在google商城下載
代碼如下:
from selenium import webdriver
from time import sleep
import dns.resolver #dns模塊
def CUrl():
try:
A = dns.resolver.query("自己的域名", 'A')
for i in A.response.answer:
for j in i.items:
print(j.address)
except:
Aikuai()
def Aikuai():
chromtexe = "C:\Program Files\Google\Chrome\Application\chromedriver"
driver = webdriver.Chrome(chromtexe)
driver.implicitly_wait(10) #設定超時時間
driver.maximize_window() #視窗最大化顯示
driver.get("http://192.168.1.1/login#/login")
#用戶名
search_name = driver.find_element_by_xpath("/html/body/div[@id='app']/div[@class='body_PE']/div[@class='login clearfix']/form/ul[@class='load_ul']/li[@class='text_center'][1]/div[@class='div_info']/input[@id='usernameIpt']")
search_name.clear()
search_name.send_keys("admin")
#密碼
search_passwd = driver.find_element_by_xpath("/html/body/div[@id='app']/div[@class='body_PE']/div[@class='login clearfix']/form/ul[@class='load_ul']/li[@class='text_center'][2]/div[@class='div_info']/input[@class='password inptText']")
search_passwd.clear()
search_passwd.send_keys("admin123456")
#登錄
driver.find_element_by_xpath("/html/body/div[@id='app']/div[@class='body_PE']/div[@class='login clearfix']/form/ul[@class='load_ul']/li[@class='text_center'][3]/button[@class='btn btn_green']").click()
#占不系結
driver.find_element_by_xpath("/html/body/div[@id='app']/div/div[2]/div[@class='notice_box']/div/div[3]/a").click()
#停用
driver.find_element_by_xpath("/html/body/div[@id='app']/div/div[1]/div[@id='fantasyMenu']/div[@class='back_section']/div[@class='back_section_scroll js_back_section_scroll main_section']/div[@class='qaq3']/div[@class='main_section']/div[@class='wrapper row']/div[@class='box clearfix']/div/div[2]/div[@class='table_box clearfix']/table[@class='table table_MAX checkbox_checked']/tbody/tr[2]/td[@class='td_opear']/p/a[2]").click()
sleep(3)
driver.refresh() #重繪
#啟用
driver.find_element_by_xpath("/html/body/div[@id='app']/div/div[1]/div[@id='fantasyMenu']/div[@class='back_section']/div[@class='back_section_scroll js_back_section_scroll main_section']/div[@class='qaq3']/div[@class='main_section']/div[@class='wrapper row']/div[@class='box clearfix']/div/div[2]/div[@class='table_box clearfix']/table[@class='table table_MAX checkbox_checked']/tbody/tr[2]/td[@class='td_opear']/p/a[2]").click()
#關閉瀏覽器視窗
driver.quit()
driver.close()
if __name__ == "__main__":
CUrl()
啟動內容就不附上了,自行測驗,
打包成exe檔案,并設定周期計劃任務,
pip install pyinstaller
cd 到專案路徑
pyinstaller -F huashengke.py
在dist檔案夾可找到exe檔案

windows添加周期性計劃任務:
控制面板\所有控制面板項\管理工具\任務計劃程式

創建基本任務,添加程式,設定周期時間,







完成!!!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/377043.html
標籤:其他
