-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
68 lines (62 loc) · 2.75 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# If you want to install the driver automatically, import:
# from webdriver_manager.chrome import ChromeDriverManager
# then define the driver like this:
# driver = webdriver.Chrome(ChromeDriverManager().install())
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import configparser
import os
import time
from pythonping import ping
def restart(binary_location,driver_location,password):
print("Start restarting process")
try:
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--window-size=1920,1080")
chrome_options.add_argument("--ignore-certificate-errors")
chrome_options.binary_location = binary_location
driver = webdriver.Chrome(options=chrome_options)
driver.delete_cookie("192.168.100.1")
driver.get("https://192.168.100.1")
driver.find_element(By.ID,"Password").send_keys(password)
driver.find_element(By.ID,"LoginBtn").click()
if driver.find_element(By.ID,"InvalidMsg").is_displayed():
print("Wrong password")
exit()
try:
driver.find_element(By.XPATH,"//input[@type='button' and @value='OK']")[0].click()
except:
pass
driver.get("https://192.168.100.1/?status_restart&mid=StatusRestart")
WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.ID, "PAGE_RESTART_RESTART"))
)
driver.find_element(By.XPATH,"//input[@type='button' and @id='PAGE_RESTART_RESTART']").click()
driver.find_element(By.XPATH,"//input[@type='button' and @id='PAGE_RESTART_POPUP_APPLY1']").click()
print("Restart successfully!")
finally:
driver.quit()
def main():
try:
response_list = ping("8.8.8.8",timeout=5,count=5)
if not response_list.success(2):
dir_path = os.path.dirname(os.path.realpath(__file__))
config = configparser.ConfigParser()
config.read(dir_path + '/config.cfg')
binary_location = config['default']['binary_location']
driver_location = config['default']['driver_location']
password = config['default']['password']
restart(binary_location,driver_location,password)
else:
print("Internet connection is UP. Nothing to do.")
except (OSError,RuntimeError) as e:
print(format(e))
if __name__ == "__main__":
main()