Skip to content

Instantly share code, notes, and snippets.

@sekomer
Created August 4, 2024 10:10
Show Gist options
  • Select an option

  • Save sekomer/e0e0fc76f29099dd88f505b98e8df432 to your computer and use it in GitHub Desktop.

Select an option

Save sekomer/e0e0fc76f29099dd88f505b98e8df432 to your computer and use it in GitHub Desktop.
reboot tplink modem using selenium and chrome web driver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver_path = "/opt/homebrew/bin/chromedriver"
driver = webdriver.Chrome(executable_path=driver_path)
login_url = "http://192.168.1.1"
# todo: change these
username = "admin"
password = "turktelekom"
try:
driver.get(login_url)
print("Logging in...")
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "userName"))
).send_keys(username)
driver.find_element(By.ID, "pcPassword").send_keys(password)
login_button = driver.find_element(By.ID, "loginBtn")
ActionChains(driver).click(login_button).perform()
WebDriverWait(driver, 10).until(EC.alert_is_present())
driver.switch_to.alert.accept()
print("Login button clicked")
try:
skip_button_1 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "skipBtn"))
)
skip_button_1.click()
except Exception as e:
print("First skip button not found or clicked:", e)
try:
skip_button_2 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(By.XPATH, "/html/body/div[2]/div/div/div[2]/div[2]")
)
)
skip_button_2.click()
except Exception as e:
print("Second skip button not found or clicked:", e)
print("Login successful")
menu_tools = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, "menu_tools"))
)
menu_tools.click()
menu_restart = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, "menu_restart"))
)
menu_restart.click()
reboot_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, "button_reboot"))
)
ActionChains(driver).click(reboot_button).perform()
WebDriverWait(driver, 10).until(EC.alert_is_present())
driver.switch_to.alert.accept()
print("Reboot initiated")
time.sleep(1)
print("Reboot completed")
finally:
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment