Why would I add multiple requests

In this improvement scheme, Selenium mainly undertakes to identify the title of the web page and make a judgment. As I mentioned in the last article, some of the post-login identification in my code is flawed. It suddenly occurred to me today that a simpler way can be used for verification, that is, to visit baidu home page. For accessing such a simple website, it is a little too much work to open a window again using Selenium, so I used Requests to write this section. At a stroke, you can fully express what Selenium takes a long time to handle in just two lines.

r = requests.get('http://www.baidu.com')
		if r.status_code == 200:
Copy the code

Status_code is available in web page parsing. For more details, see this article. Where status_code=200 is commonly used, which means that the page can be accessed normally.

Inspect the main parts of the code

Def Is_OK () : global IsCode # judge jump interface driver. Execute_script (" window. The scrollTo (0, document. Body. ScrollHeight);" ) page_information = driver.find_element_by_css_selector('head > title').get_attribute('textContent') if Page_information == 'Authentication success page ': R = requests. Get ('http://www.baidu.com') #print(r.tatus_code) if r.tatus_code == 200: IsCode = 0 print("login successfully") else: IsCode = 1 quit() login(account, password) elif page_information == 'information page ': Back () login(account, password) IsCode = 1 elif page_information == 'login window ': login(account, password) IsCode = 1 return IsCodeCopy the code

As you can see, I’m using page authentication + access authentication. If your computer displays an authentication success page, I need to check whether you are connected to the Internet. If your computer still displays an information page or a login window, I don’t need to check the connection for now, because the code doesn’t do what you need. About this idea, I also used in the first three window recognition, detection if your authentication success page is also a network, I will take the initiative to quit the program, so as to save everyone’s time.

PS: If you need Python learning materials, please click on the link below to obtain them

Free Python learning materials and group communication solutions click to join

Cycle of detection

I used a while loop for detection, because I don’t know how many times the detection can be successful, but I set the maximum detection of 4 times. In actual use, the maximum number of detection is only 2 times. When the fourth detection is needed, it means that the network is disconnected in the middle of the night or your account password is wrong, which requires you to judge. About account password mistyped, of course, this kind of situation, in addition to manually delete the code generation TXT file, you can also set up some methods in the code, after testing four times, before returning automatically, and then allow you to re-enter your account password, to cover a local file, this situation is due to the occurrences is too little, I didn’t write, interested friends can study, Add to this code.

All the code

#login_selenium.py from selenium import webdriver from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait from selenium.common.exceptions import TimeoutException from selenium.webdriver.common.by import By import time, os, This function mainly realizes the identification of the three login Windows and saves the account and password locally. Through the first login, you need to enter the account password. After login, it can be completed with one key, and the time is also very fast. Currently, there are no bugs in the test. ''' def txt_handle(): global account, password try: F = open(' TXT ', "r", Encoding =" utF-8 ") lines = f.readlines() print(" enter ") account = lines[0] account = account.replace('\n', encoding=" utF-8 ") lines = f.readlines() print(" enter ") account = lines[0] account = account.replace('\n', '') password = lines[1] f.close() #print(account, password) except Exception as e: Password = input(" Please input password: TXT = account + '\n' + password print(account, password) f = open(' Encoding =" utF-8 ") f.rite (TXT) print(" encoding=" utF-8 ") f.rite (TXT) print(" ) f.close() return account, password def login(account, password): #print(account, password) input_account = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '#edit_body > div.edit_row.ui-resizable-autohide > div.edit_loginBox.ui-resizable-autohide > form > input:nth-child(3)'))) input_password = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '#edit_body > div.edit_row.ui-resizable-autohide > div.edit_loginBox.ui-resizable-autohide > form > input:nth-child(4)'))) button = driver.find_element_by_css_selector('#edit_body > div.edit_row.ui-resizable-autohide > div.edit_loginBox.ui-resizable-autohide > form > input:nth-child(2)') input_account[0].send_keys(account) Input_password [0].send_keys(password) #print(" ready to click ") button.click() def back(): button_back = driver.find_element_by_css_selector('#edit_body > div > div.edit_loginBox.ui-resizable-autohide > form > input') button_back.click() def quit(): button_quit = driver.find_element_by_css_selector('#edit_body > div > div.edit_loginBox.ui-resizable-autohide > form > Input ') button_quite.click () time.sleep(2) confirm = driver.switch_to.alert confirm.accept() print(' you just confirmed to be offline ') Time. Sleep (5) confirm. Accept () print(' now return ') def Is_OK(): Global IsCode # judge jump interface driver. Execute_script (" window. The scrollTo (0, document. Body. ScrollHeight);" ) page_information = driver.find_element_by_css_selector('head > title').get_attribute('textContent') if Page_information == 'Authentication success page ': R = requests. Get ('http://www.baidu.com', timeout =30) #print(r.tatus_code) if r.tatus_code == 200: IsCode = 0 print("login successfully") else: IsCode = 1 quit() login(account, password) elif page_information == 'information page ': Back () login(account, password) IsCode = 1 elif page_information == 'login window ': login(account, password) IsCode = 1 return IsCode def Is_page(): # judge jump interface driver. Execute_script (" window. The scrollTo (0, document. Body. ScrollHeight);" ) page_information = driver.find_element_by_css_selector('head > title').get_attribute('textContent') if Page_information == 'login window ': login(Account, password) Back () login(Account, password) elif page_information == 'Authentication success page ': R = requests. Get ('http://www.baidu.com', timeout =30) if r = requests. Get ('http://www.baidu.com', timeout =30) if r = requests. Quit () login(account, password) # check whether the login page is displayed. Is_OK() = 0: count = count + 1 Is_OK() if IsCode == 1: print(" %s "% STR (count)) else: % STR (count)) if count == 4: print(" % STR (count) ") ) break url = 'http://p.njupt.edu.cn' driver = webdriver.Chrome('chromedriver.exe') wait = WebDriverWait(driver, 30) driver.get(url) account = 'XXXXXXXXXX' password = 'XXXXXX' IsCode = 1 txt_handle() #print(account, Password) Is_page() driver.quit() print("5 seconds later the window will automatically close ") time.sleep(4) os.sys.exit()Copy the code