Since I wrote the campus network software last time, I want to optimize and modify it in depth.
Optimized content:
1. If there is a problem with the browser, it will report an error directly, but now it is all under control.
2. After logging in to the browser, click the cross and it will freeze and close.
3. The browser call speed is slow and the overall code reuse is low.
4. Put the account number and password at the front for easy modification
New content:
1. This time, a new class writing method is adopted, using python Selenium 4.15.2 to write.
2. This time, it supports modifying the campus network address and customizing login content.
Originally I wanted to directly call the chromedrive in the folder, but later I thought it would be better to call the system’s own one.
I won’t give too much introduction this time. If you want to read the introduction, please read the previous issue https://blog.nuoyis.net/1053.html
Let’s look directly at the code this time
main.py
import getpass import os importsys import time from nuofunction import lianwang if __name__ == "__main__": # Campus network definition xiaoyuanurl = "http://172.17.1.2" #Campus network account xiaoyuanusername = "" #Campus network password xiaoyuanpassword = "" #Introduce class nuo = lianwang() #Campus network connectivity detection if not (nuo.geturl(xiaoyuanurl)): print("The network is abnormal or not the campus network, please check") exit(1) # Password verification (to prevent stolen logins) # sslogin = input("Please enter the assigned security password:") password = getpass.getpass("Please enter the assigned security password:") if password != "nu23o09": print("Wrong password") input("Press any key to close") exit(1) # After successful verification, start checking whether you have logged in if not (nuo.openurl(xiaoyuanurl)): print("Browser kernel call exception, please check") input("Press any key to close") exit(1) print("Silent login in progress, please wait....") if True in nuo.find("xpath", "/html/body/div/div[1]/div[5]/input"): print("Login detected, start execution") nuo.shell("xpath", "click", "/html/body/div/div[1]/div[5]/input") else: print("No login detected, continue execution") # username nuo.shell("xpath", "click", "/html/body/div/div[1]/form/input[1]") nuo.shell("xpath", "clear", "/html/body/div/div[1]/form/input[1]") nuo.shell("xpath", "element", "/html/body/div/div[1]/form/input[1]", xiaoyuanusername) # password nuo.shell("xpath", "click", "/html/body/div/div[1]/form/input[2]") nuo.shell("xpath", "clear", "/html/body/div/div[1]/form/input[2]") nuo.shell("xpath", "element", "/html/body/div/div[1]/form/input[2]", xiaoyuanpassword) nuo.shell("xpath", "click", "/html/body/div/div[1]/form/button") # Close the browser kernel nuo.closeurl() print("Login completed, system browser is being called for you to check whether login is successful") os.popen("start " + xiaoyuanurl) input("Press any key to close")
nuofunction.py
# Part of the writing method can be borrowed from https://zhuanlan.zhihu.com/p/191566983 import os import requests import time import urllib3 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options as ChromeOptions from selenium.webdriver.firefox.options import Options as FirefoxOptions from selenium.webdriver.edge.options import Options as EdgeOptions class lianwang: def geturl(self, url): try: http = urllib3.PoolManager() http.request('GET', url) return 1 except Exception as e: return 0 def touoptions(self): #Header settings self.options.add_argument("--incognito") self.options.add_argument('--no-sandbox') self.options.add_argument('--headless') self.options.add_argument('--disable-gpu') self.options.add_argument('--ignore-certificate-errors') self.options.add_experimental_option("excludeSwitches", ["enable-automation"]) self.options.add_experimental_option("useAutomationExtension", False) return True def openurl(self, url): """ open the Web page :param url: :return: Return to webdriver """ # Google browser judgment try: print("Using Google Chrome") self.options = ChromeOptions() self.touoptions() # service = Service("./chromedriver.exe") # os.path.join(os.path.abspath("."), "chromedriver.exe")) # CHROMEDRIVER_PATH = "./chromedriver.exe" # service = Service(executable_path=CHROMEDRIVER_PATH) # self.driver = webdriver.Chrome(options=options) self.driver = webdriver.Chrome(options=self.options) self.driver.get(url) return True except: print("Google Chrome call failed, calling other browsers") # Firefox browser judgment try: print("Using Firefox") self.options = FirefoxOptions() self.touoptions() # service = Service("./chromedriver.exe") # os.path.join(os.path.abspath("."), "chromedriver.exe")) # CHROMEDRIVER_PATH = "./chromedriver.exe" # service = Service(executable_path=CHROMEDRIVER_PATH) # self.driver = webdriver.Firefox(options=options) self.driver = webdriver.Firefox(options=self.options) self.driver.get(url) return True except: print("Firefox browser failed to call, calling other browsers") #Edge browser judgment try: print("Using Edge browser") self.options = EdgeOptions() self.touoptions() # service = Service("./chromedriver.exe") # os.path.join(os.path.abspath("."), "chromedriver.exe")) # CHROMEDRIVER_PATH = "./chromedriver.exe" # service = Service(executable_path=CHROMEDRIVER_PATH) # self.driver = webdriver.Firefox(options=options) self.driver = webdriver.Edge(options=self.options) self.driver.get(url) return True except: print("Edge browser call failed") return False def find(self, type, text): """ Find web page elements """ try: if type == 'id': elem = self.driver.find_element(By.ID, text) elif type == 'name': elem = self.driver.find_element(By.NAME, text) elif type == 'class': elem = self.driver.find_element(By.CLASS_NAME, text) elif type == 'xpath': elem = self.driver.find_element(By.XPATH, text) elif type == 'css': elem = self.driver.find_element(By.CSS_SELECTOR, text) else: return False, 0 except Exception as e: return False, 0 return True, elem def shell(self, type1, type2, text1, text2=''): """ Comprehensive judgment and execution area """ _isOK, _strLOG = self.find(type1, text1) if not _isOK: # The element is not found and a failure result is returned. return False elem = _strLOG # Click if type2 == "click": try: elem.click() exceptException: return False return True # Clear the contents of the box elif type2 == "clear": try: elem.clear() exceptException: return False return True # input elif type2 == "element": try: elem.send_keys(text2) exceptException: return False return True def closeurl(self): self.driver.quit()
If you want to quote, please retain the copyright below
Article link: https://blog.nuoyis.net/1076.html
Copyright Statement: Unless otherwise stated, all articles on this blog are licensed under the CC BY-NC-SA 4.0 license. Please indicate when reprinting it from Shuzhizhihai!