Home >Backend Development >Python Tutorial >Python+selenium implements simple automatic check-in function for epidemic information

Python+selenium implements simple automatic check-in function for epidemic information

coldplay.xixi
coldplay.xixiforward
2020-08-22 17:21:222695browse

Python+selenium implements simple automatic check-in function for epidemic information

[Related learning recommendations: python video tutorial]

Since the school requires us to check in and sign in the epidemic information on the official website every day, more or less It takes 1 minute to operate. The dignity of programmers tells us that we must not clock in manually. I happened to have learned selenium recently, so I spent 5 minutes drawing and writing a small program to automatically clock in and sign in.

Test environment: python3.7, selenium, chrome browser

The configuration of seleium and chromedriver will not be discussed here. Here is a link

First find the school information Portal login page:

Python+selenium implements simple automatic check-in function for epidemic information


#导入selenium中的webdriver
from selenium import webdriver
import time
url = 'http://my.hhu.edu.cn/login.portal' #信息门户的登陆页面
driver = webdriver.Chrome() # 初始化一个Chrome的驱动
driver.get(url) # 让自动化模块控制的Chrome浏览器跳转到信息门户登陆页面

It’s time to simulate login. First find the input box for the user name. Press ctrl shift c to open the developer tools, click the input box to the right of the user name, and you can find the code corresponding to the input box in the developer tools on the right.

Python+selenium implements simple automatic check-in function for epidemic information
Python+selenium implements simple automatic check-in function for epidemic information

Right-click the module and click copy->copy Xpath. (Xpath is used to locate the input control)

root = '' #赋值自己的用户名
password = '' # 赋值自己的密码
driver.find_element_by_xpath('//*[@id="username"]').send_keys(root) #将xpath赋值在前面的括号中,通过send_keys方法给input赋值

#类似的,赋值密码框的xpath,赋值密码
driver.find_element_by_xpath('//*[@id="password"]').send_keys(password)

After entering the account password, it is time to click to log in. Press ctrl shift c, click the login button, right-click the code block corresponding to the developer tools on the right and copy->copy xpath to obtain the xpath of the button.

driver.find_element_by_xpath('//*[@id="changeBack"]/tbody/tr/td[2]/table[1]/tbody/tr[2]/td/p/input[1]').click()
#通过click方法点击登录框,跳转到登陆后的页面

Python+selenium implements simple automatic check-in function for epidemic information

On the page after logging in, I found the health reporting function box. Click on the function box and find that the page jumps to the check-in page: Python+selenium implements simple automatic check-in function for epidemic information

Copy the URL of the page and let the program jump to the page after logging in:

form = 'http://form.hhu.edu.cn/pdc/form/list'
driver.get(form)

Let the program click " Undergraduate health check-in:

driver.find_element_by_xpath('/html/body/p[1]/p[4]/p/section/section/p/a/p[2]').click()

will jump to the following page

Python+selenium implements simple automatic check-in function for epidemic information

Click submit to complete the check-in

driver.find_element_by_xpath('//*[@id="saveBtn"]').click()

Complete procedure :

from selenium import webdriver
import time
root = ''
password = ''
url = 'http://my.hhu.edu.cn/login.portal'
driver = webdriver.Chrome()
driver.get(url)
driver.find_element_by_xpath('//*[@id="username"]').send_keys(root)
driver.find_element_by_xpath('//*[@id="password"]').send_keys(password)
driver.find_element_by_xpath('//*[@id="changeBack"]/tbody/tr/td[2]/table[1]/tbody/tr[2]/td/p/input[1]').click()
form = 'http://form.hhu.edu.cn/pdc/form/list'
driver.get(form)
driver.find_element_by_xpath('/html/body/p[1]/p[4]/p/section/section/p/a/p[2]').click()
driver.find_element_by_xpath('//*[@id="saveBtn"]').click()

Related learning recommendations: Programming video

The above is the detailed content of Python+selenium implements simple automatic check-in function for epidemic information. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete