A zero-based ten minutes to understand the automatic punch-in program in super detail! !

This article uses python's selenium module to implement an automatic card punching program


The structure of this article: first introduce three preparatory knowledge, the big guys can skip it. Then get started directly with the code (because this code is really super super simple). But not recommended no contact with the module next to the Friends of the direct look at the code . After all, different webpage processing methods are still different.

This article introduces the principle in as much detail as possible, and is dedicated to allowing readers to write programs for various check-in webpages after reading.

Introduction to selenium module

This module allows us to use the py program to operate mainstream browsers such as Chrome and Firefox to achieve automated testing.

If we want the computer to help us do something with the browser, we must tell it where to click first, what to enter, and then where to click. This "where" is achieved through the positioning of the selenium module:

It provides eight positioning methods , interested friends can take a look. Here we mainly introduce the three types used in the program: (The specific usage method is explained in the code part)

method effect
find_element_by_id() Positioning by the id of the element
find_element_by_name() Locate by the name of the element
find_element_by_xpath() Locate by element's xpath

What is this id, namesum xpath? In fact, it is the attribute of a certain content in a web page.

We know that a web page is composed of many elements, such as the blog you are viewing, which includes the blogger’s profile picture, the text you are reading, the like button in the lower left corner (crazy express), etc. When programmers write web pages, for ease of use distinguish these elements will add different elements idand name, as our name suggests.

nameAnd the iddifference is that, namelike our name, and idlike our ID number. There may be people with the same name but without the same ID number. (This is not rigorous, but let's understand it this way. Other differences are not very important to this program. ) It is worth noting that not every element needs to have nameor idattributes. So we still need a third method of XPathpositioning

For example, the blog you are browsing, press F12, click to elementview the source code, some tags are like this: what we said, for
Insert picture description here
example, this div tag has only idattributes.
Insert picture description here
You can see that the input tag in the picture above has both idthis attribute and namethis attribute. .

Next install selenium:

pip install Selenium

Browser driver

Selenium itself does not have the ability to open the browser, and requires the driver cooperation of each browser. Next we install a browser driver: check the version of your browser before downloading!
The Chrome browser version corresponds to
various versions of Chrome for driver download.
Other browsers can download the address from Baidu.

FirefoxDriver and other driver download and driver configuration

After downloading the browser driver, you also need to configure the environment variables so that your program can find the driver: (The path stored in the environment variable tells the computer where to find it when you need to run a program. The system is receiving After running a command to a program, you will go to the environment variable table one by one to see if there is any program you want)

My Computer -> Properties -> System Settings -> Advanced -> Environment Variables -> System Variables -> Path, add your drive storage path such as "F:\ChromeDriver" directory to the value of Path. For example: Path field; F:\ChromeDriver

After configuring the environment variables, you can use the following code to obtain the browser driver driver:

 driver = webdriver.Chrome()

Simulate mouse click and fill

Take the following webpage as an example. I need to fill in my student ID, password and click the "Login" button. So first of all, how do you use the program to locate these positions that need to be filled in/clicked? Use the method described above!
Insert picture description here
F12Find the Elements source code, then click the mouse icon on the left side of the figure below, and then click the position where you need to enter the student ID in the figure above-look for the source code of this element.
Insert picture description here
We found this:
Insert picture description here
you can see that this tag has a name attribute and its value userid. Then we use its name to locate it! Code:

driver.find_element_by_name('userid').send_keys('你的学号')

The above picture driveris the browser driver obtained at the driver installation of this article

One line of code is enough to fill in the student ID! Is not it simple? ! For other places we need to fill in, just do the same.

nameWhat should I do if I encounter a label with no attributes? For example, the following "Login" button:
Insert picture description here
It doesn't matter! It has id. Let us use idto locate:

driver.find_element_by_id('formSubmitBtn').click()#点击登录

XPath positioning

Some position no idno name, only class, how to do, do not use classto locate? But other elements also have the same class. Then we can only use xpathit (in fact, it should be no problem to use xpath for the previous positioning, because each page element must have a unique xpath )

How to find the xpath of a certain input box: Right-click the code of the input box to open the menu and operate as shown below: After Insert picture description here
finding the xpath of an input box, we store it in a variable (because direct use will be very long... of course. It’s no problem if you paste it directly into the corresponding location)

movement = "/html/body/div[1]/form/div[3]/div[2]/label[1]"#当日没有移动的按钮
driver.find_element_by_xpath(movement).click()#点选按钮

A few details

After filling in the check-in information for the blogger’s example, you need to click "Submit" and then a confirmation window will pop up after a while , and you need to confirm a submission. But while clicking the "Submit" button, the confirmation window has not yet appeared. So we use a sleep between the two clicks to pause the program for a while, wait for this window to come out, and then click to confirm.

driver.find_element_by_xpath(submit).click()#点击提交
time.sleep(2)
driver.find_element_by_xpath(check).click()#点击确定

Complete code

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
from pyrsistent._transformations import inc

# 只需修改下面三项即可 学号 密码 打卡网址
username = "登陆账号"
password = "对应密码"
url = "此处填写打卡网址" 

# 下面是每次填报的时候几个需要填写的地方,F12找到这些位置的 full XPath
movement = "/html/body/div[1]/form/div[3]/div[2]/label[1]"#有无移动
touch = "/html/body/div[1]/form/div[4]/div[2]/label[1]"#有无接触史
condition = "/html/body/div[1]/form/div[5]/div[2]/label[1]"#健康状况
quarantine = "/html/body/div[1]/form/div[6]/div[2]/label[1]"#是否隔离
inCollege = "/html/body/div[1]/form/div[7]/div[2]/label[1]"#是否在校
submit = "/html/body/div[1]/form/div[8]/a"#提交
check = "/html/body/div[3]/div[2]/div[2]/a[2]"#最后的确定按钮

# 模拟登陆打卡
def do_login(driver):
    # driver.maximize_window() 将窗口最大化
    # 找到登录框 输入账号密码
    driver.find_element_by_name('userid').send_keys(userName)
    driver.find_element_by_name('userpwd').send_keys(password)
    driver.find_element_by_id('formSubmitBtn').click()#点击登录

    driver.find_element_by_xpath(movement).click()
    driver.find_element_by_xpath(touch).click()
    driver.find_element_by_xpath(condition).click()
    driver.find_element_by_xpath(quarantine).click()
    driver.find_element_by_xpath(inCollege).click()
    
    driver.find_element_by_xpath(submit).click()#点击提交
    time.sleep(2)
    driver.find_element_by_xpath(check).click()#点击确定
    time.sleep(1)

if __name__ == '__main__':
    # 模拟浏览器打开网站
    driver = webdriver.Chrome()
    driver.get(url)
    # 登录并打卡
    do_login(driver)
    print("打卡结束")
    time.sleep(1)#终端给你时间确认已经打卡成功
    driver.quit()

After writing the program, you can type it into an exe. It is also very convenient to add it to start up, or to start it manually without adding it to the desktop, hahaha. Package exe tutorial

Disclaimer: This blog is only for learning and communication, please do not use the content written in this blog for illegal actions. This blogger does not bear all the consequences caused

Configure to the server to realize automatic punch-in

Since I finished writing this program, I have been thinking, if one day I don't use the computer all morning, would I miss the time to check in. Or it is more troublesome to turn on the computer once to check in. The big guy provided an idea: put the program directly on the server and start it regularly. Hahahaha! (Obviously it's my own food, I can't even think of it)

So I have the following blog to configure my punch card program on the server to complete the automatic operation so easy~

Guess you like

Origin blog.csdn.net/weixin_44559752/article/details/107634627