In order not to get up early, clock in and sleep late, I sacrificed selenium

preface Every day I fantasize about how wonderful life would be if I could get money without going to work! It's not win...
1. Import and storage
2. Procedure flow
3. Specific code
4. Code interpretation
5. Introduction to XPath

preface Every day I fantasize about how wonderful life would be if I could get money without going to work! It's not winter. For me and other night owls, it's really painful to get up early and punch in every day. I often bother my friends to punch in for me because I don't want to get up. Finally I think of the killer mace! 1, Python+Selenium

Selenium is a tool that uses computer to simulate people to operate browser web pages, which can realize automation. It is estimated that many children know something when learning reptiles. The installation steps are also simple:

pip install selenium #If not, go ahead sudo pip install selenium #If not, just pip3 install selenium #Just sudo pip3 install selenium

In addition to the installation library, there should also be a browser launched by selenium program, so the program running environment needs to install firefox browser and download geckodriver in advance

wget https://github.com/mozilla/geckodriver/releases/download/v0.22.0/geckodriver-v0.22.0-linux64.tar.gz Of each version driver The download address is as follows https://github.com/mozilla/geckodriver/releases tar zxvf geckodriver-v0.22.0-linux64.tar.gz mv geckodriver /usr/local/bin
2, Use steps

1. Import and storage

The code is as follows:

import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC

2. Procedure flow

  • Open the web page;
  • Identify the position of the account and password box and enter the account and password;
  • Simulate click login
  • Simulated click clock out

3. Specific code

The code is as follows:

#!/usr/bin/env python # -*- coding: utf-8 -*- import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver import ActionChains class LoginUrl(object): #Initialize the properties of the class def __init__(self, driver, url, username, password): self.__driver = driver self.__url = url self.__username = username self.__password = password #How to open a web page def openwebsite(self): self.__driver.maximize_window() self.__driver.get(self.__url) #Enter web account def inputusername(self, find_element_method, element): if(find_element_method == "id"): WebDriverWait(self.__driver, 10).until(EC.presence_of_element_located((By.ID, element)))#Wait for 10s and query every 500ms until the element is loaded or more than 10s usrName = self.__driver.find_element_by_id(element) elif(find_element_method == "name"): WebDriverWait(self.__driver, 10).until(EC.presence_of_element_located((By.NAME, element))) usrName = self.__driver.find_element_by_name(element) elif(find_element_method == "xpath"): WebDriverWait(self.__driver, 10).until(EC.presence_of_element_located((By.XPATH, element))) usrName = self.__driver.find_element_by_xpath(element) else: print("find element error!") usrName.send_keys(self.__username) #Enter web password def inputpassword(self, find_element_method, element): if(find_element_method == "id"): WebDriverWait(self.__driver, 10).until(EC.presence_of_element_located((By.ID, element))) passWrd = self.__driver.find_element_by_id(element) elif(find_element_method == "name"): WebDriverWait(self.__driver, 10).until(EC.presence_of_element_located((By.NAME, element))) passWrd = self.__driver.find_element_by_name(element) elif(find_element_method == "xpath"): WebDriverWait(self.__driver, 10).until(EC.presence_of_element_located((By.XPATH, element))) passWrd = self.__driver.find_element_by_xpath(element) else: print("find element error!") passWrd.send_keys(self.__password) #Click login def clicksubmit(self, find_element_method, element): if(find_element_method == "id"): WebDriverWait(self.__driver, 5).until(EC.presence_of_element_located((By.ID, element))) self.__driver.find_element_by_id(element).click() elif(find_element_method == "name"): WebDriverWait(self.__driver, 5).until(EC.presence_of_element_located((By.NAME, element))) self.__driver.find_element_by_name(element).click() elif(find_element_method == "xpath"): WebDriverWait(self.__driver, 5).until(EC.presence_of_element_located((By.XPATH, element))) self.__driver.find_element_by_xpath(element).click() else: print( "find element error!") #test def test(self, find_element_method, element): ActionChains(self.__driver).move_to_element(self.__driver.find_element_by_xpath(element)).perform() self.__driver.find_element_by_xpath(element).click() def main(): #Log in to the clock in website browser1 = webdriver.Firefox() giga = LoginUrl(browser1, "http://Xxxxxxxxx / index. HTML ", u" your account name ", u" your password ") giga.openwebsite() # Enter account number giga.inputusername("id", "loginid") # Input password giga.inputpassword("id", "userpassword") # Click login giga.clicksubmit("xpath", "/html/body/div[@id='container']/div[@id='LoginContainer']/div[@class='e9login-content']/div[@class='e9login-element e9login-btn']/div[@class='e9login-form-submit e9login-submit']/button[@id='submit']") # Wait for completion time.sleep(3) # Click attendance giga.clicksubmit("xpath", "/html/body/div[@id='container']/div/div[@class='e9theme-layout-container ']/div[@class='e9theme-layout-header']/div[@class='e9header-container wea-f12']/div[@class='e9header-right']/div[@class='e9header-right-toolbar']/div[@id='signPlugin']/div[@class='singBtn']/span") # Wait for completion time.sleep(3) # Update the clock in time. The previous problem should be written for Xphth. Just simplify it giga.test("xpath", "//a[@class='resign']") print("succeess...") if __name__ == "__main__": main()

4. Code interpretation

  1. Starting from the main function, first log in to the website and use your own website, user name and password to replace the part in the code. Normally, when we log in manually, we don't need to input it repeatedly due to the problem of cookie s, but whether it is called or re entered here.
  2. The next step is to enter the account and password. The parameters "id" and "login id" are not universal. You need to log in to the website manually, use the F12 developer tool to view the source code of the website and find the corresponding label. Just right click - "check" in your login box to see the corresponding source code. If you can see "id" in the code "Tags and corresponding values can be found by id. similarly, if you have a" name "tag and corresponding values, you can also use name. If you don't have both, you can also find them through Xpath. Please refer to section 5 for details. You don't need to understand it at all. There are special tools to help you realize it.
  3. The implementation logic of account and password filling box should be similar. They all provide three positioning methods, id tag, name tag and rough Xpath positioning.
  4. When the program locates the account and password box through one of the three methods, the program needs to help us fill in the account and password, that is, usrname.send_keys (self. _username) and passwrd.send_keys (self. _password)
  5. After filling in the text, it is natural to click the login button. This time, a different method is used, that is, the simple and crude Xpath method. A large string of the second parameter is actually generated by a special tool and can be used directly by copying it in
  6. The remaining operations such as clicking on attendance, clicking on punch in and so on are the specific click steps. The order and mode of different websites are different, but the logic is the same for those that only need to click

5. Introduction to XPath

1. Download the file xpath-helper.crx, link: https://pan.baidu.com/s/1dFgzBSd Password: zwvb, thank you
2. Find the "extender" option menu in Google browser or Edge browser.
3. Then you will enter the extension interface. Drag the downloaded offline plug-in xpath-helper.crx to the extension interface. It will give a prompt and release the mouse.
4. After the addition is successful, you can use the shortcut key Ctrl+shift+x to call up the interface and write xpath. Press it again to turn it off. Open the xpath plug-in, press and hold shift, and click all elements on the web page with the mouse to automatically generate xpath elements for you to copy and paste.

summary

For complex xpath, copying and pasting directly may cause the program to fail to locate elements. At this time, it may be necessary to simplify the things generated by the xpath helper. How to simplify, for example
/html/body/div[@id='container']/div[@id='LoginContainer']/div[@class='e9login-content']/div[@class='e9login-element e9login-btn']/div[@class='e9login-form-submit e9login-submit']/button[@id='submit']
Directly abbreviated as
//button[@id='submit']

2 December 2021, 21:19 | Views: 1224

Add new comment

For adding a comment, please log in
or create account

0 comments