python मा selenium को प्रयोग browser automation अथ

वा ब्राउज रलाई आँफै चलाउन प्रयोग हुन्छ। तेस्तै आज हामी python प्रयोग गरेर wifirouter reboot गर्न प्रयोग गर्ने छौ. सबैभन्दा पहिला तपाईंको सिस्टममा python install हुनुपर्छ तेस्तै अब तपाईं https://chromedriver.chromium.org/downloads/ मा गएर आफ्नो chrome भर्सन अनुसार ड्राइभर download गर्नुस्। अब त्यो फाइललाई unzip गरेर तेस्भित्रको फाइललाई तपाईंले सम्झिने फोल्डरमा राख्नुहोस्। अब cmd कमाण्डलाइनमा गएर pip install selenium टाईप गर्नुहोस् र केहीबेर मै सेलेनियम install हुनेछ।

अब एउटा फोल्डर बनाउनुहोस् र तपाईंको code editor मा open गर्नुहोस्। हामी main.py नामको फाइल बनाउछौ। अब तेस्भित्र यि कुराहरु लेख्छौ:

from selenium import webdriver browser = webdriver.Chrome() browser.get('https://facebook.com')
browser.get('http://192.168.1.1/login')
In [ ]:
username = 'admin'
password = 'admin'
    
user_field = browser.find_element_by_name('sth')
pass_field = browser.find_element_by_name('sth')
user_field.send_keys(username)
pass_field.send_keys(password)
In [ ]:
browser.find_element_by_css_selector("input[onclick*='return saveChanges()']").click()
In [ ]:
browser.get('http://192.168.1.1/reboot.htm/')
In [ ]:
browser.find_element_by_css_selector("input[onclick*='return rebootClick()']").click()
alert_obj = browser.switch_to.alert
time.sleep(2)
alert_obj.accept()
In [ ]:
from win_notify import ballon_tip as winnotifier
In [ ]:
winnotifier("sharmaji: Wifi rebooted",\
     "TOTOLINK succesfully has been rebooted wait for 10-15 secs")
time.sleep(3)
browser.close()
In [ ]:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--headless")
browser = webdriver.Chrome(options=chrome_options)
In [ ]:
def setup(incognito=False):
    global browser
    if incognito:
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--incognito")
        chrome_options.add_argument("--disable-extensions")
        chrome_options.add_argument("--disable-gpu")
        chrome_options.add_argument("--headless")
        browser = webdriver.Chrome(options=chrome_options)
    else:
        browser = webdriver.Chrome()
In [ ]:
def authenticate(router_user, router_pass):
In [ ]:
def authenticate(router_user, router_pass):
    browser.get("http://192.168.1.1/login.htm")
    user_field = browser.find_element_by_name('username')
    pass_field = browser.find_element_by_name('userpass')
    user_field.send_keys(router_user)
    pass_field.send_keys(router_pass)
    browser.find_element_by_css_selector("input[onclick*='return saveChanges()']").click()
In [ ]:
def reboot():
    browser.get("http://192.168.1.1/reboot.htm")
    time.sleep(1)
    browser.find_element_by_css_selector("input[onclick*='return rebootClick()']").click()
    alert_obj = browser.switch_to.alert
    time.sleep(2)
    alert_obj.accept()
    winnotifier("sharmaji: Wifi rebooted",\
     "TOTOLINK succesfully has been rebooted wait for 10-15 secs")
    time.sleep(3)
    browser.close()
In [ ]:
setup(incognito=True)
try:
    authenticate("admin", "admin")
    reboot()
except:
    reboot()
In [ ]:
import time
from selenium import webdriver
from win_notify import balloon_tip as winnotifier
 
 
def setup(incognito=False):
    global browser
    if incognito:
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--incognito")
        chrome_options.add_argument("--disable-extensions")
        chrome_options.add_argument("--disable-gpu")
        chrome_options.add_argument("--headless")
        browser = webdriver.Chrome(options=chrome_options)
    else:
        browser = webdriver.Chrome()
 
def authenticate(
        router_user, router_pass, element_name="username",
        element_password="userpass", button_onclick='return saveChanges()'):
 
    browser.get("http://192.168.1.1/login.htm")
    username = browser.find_element_by_name(element_name)
    password = browser.find_element_by_name(element_password)
    username.send_keys(router_user)
    password.send_keys(router_pass)
    browser.find_element_by_css_selector("input[onclick*='return saveChanges()']").click()
 
def reboot():
    browser.get("http://192.168.1.1/reboot.htm")
    time.sleep(1)
    browser.find_element_by_css_selector("input[onclick*='return rebootClick()']").click()
    alert_obj = browser.switch_to.alert
    time.sleep(2)
    alert_obj.accept()
    winnotifier("sharmaji: Wifi rebooted",\
     "TOTOLINK succesfully has been rebooted wait for 10-15 secs")
    time.sleep(3)
    browser.close()
 
setup(incognito=True)
try:
    authenticate("admin", "admin")
    reboot()
except:
    reboot()