tags: python,selenium,linux

background

School is in winter vacation, and I am back to the daily clocking routine of last year at home. This year, I want to study how to automatically clock in Python and put it on a Linux server to run regularly. Today, I will share my implementation method.

Bnuz clocking website address: http://xgfx.bnuz.edu.cn/xsdtfw/sys/emapfunauth/pages/welcome.do?service=%2Fxsdtfw%2Fsys%2Fswmxsyqxxsjapp%2F*default%2Fi ndex.do%3FwxType%3D1

Environment to prepare

  • Google Chrome

  • Installation:

    yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
    Copy the code
  • Check the Google version number

    google-chrome -version
    Copy the code
  • Download the corresponding version of Google Chrome driver:

  • Taobao mirror address: http://npm.taobao.org/mirrors/chromedriver

  • Select the version number and download wGET

    Wget HTTP: / / https://cdn.npm.taobao.org/dist/chromedriver/88.0.4324.96/chromedriver_linux64.zipCopy the code
    • After the download is complete, unzip it and move it to /usr/bin
    unzip chromedriver_linux64.zip
    mv chromedriver /usr/bin/
    Copy the code
    • View the ChromeDriver versionchromedriver --version

Note: The version number of the driver should be consistent with the version number of the browser (if using another browser, download the driver of this browser)

  1. Download the selenium
    pip install selenium
    Copy the code

Use Selenium in conjunction with XPATH to manipulate elements

  • Use Selenuim to simulate click, type, and clear events.
  • Use XPATH to locate elements
    • How do YOU determine XPATH?

You can determine it yourself through the DOM, or you can use the built-in features of the Google browser to quickly locate it.

Get a quick look at XPATH’s pros and cons using Google Chrome

Advantages:

Convenient and quick, small white can also easily start

Disadvantages:

(1) When page elements change with operations, using XPATH absolute positioning may fail to locate

(2) XPATH absolute positioning fails when a page has nested subpages, but Selenuim can locate elements in a variety of ways, and other ways when XPATH is not available.

  • Google’s quick way to get full XPATH:

F12 Open developer mode

The implementation code

Special reminder: this code is only used for self-study, prohibited commercial! If your health is abnormal, please stop using it and report to your school. I take no responsibility.

#Language:Python #Author: Lily #Date:2021/1/20 # Note: This code is based on the user has manually submitted "every daily safe", # because the server will save the user's important information, such as: home address, location, etc., # so this code does not need to fill in the sensitive information. Special reminder: this code is only used for self-study, prohibited commercial! If your health is abnormal, please stop using it and report to your school. I take no responsibility. from selenium import webdriver from time import sleep from email.header import Header from smtplib import SMTP_SSL import datetime import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from Selenium. Webdriver. Chrome. The options import options email # msg_to: the recipient mailbox def send (msg_to) : # I am using the MIMEMultipart mode, so I can include the picture in the email. < span style = "box-sizing: border-box; color: RGB (74, 74, 74); line-height: 22px; font-size: 13px! Important; white-space: inherit! Important;" </h2></body></html>', 'html', 'utf-8') # MSG = MIMEText(content) MSG. Attach (content) MSG ['From'] = "[email protected]" msg['To'] = msg_to try: S = smtplib.smtp_ssl ("smtp.qq.com", 465) # email server and port number s.login("[email protected]", "XXXXXXXXXXXXXXXX ") # You need to get it by yourself in the QQ mailbox webpage. S.sendmail ("[email protected]", msg_to, msg.as_string()) print(" send successfully ") except: print(" send failed ") finally: S.quuit () # user class, which saves the user's account and password on the clock-in page, as well as the notification email address. class Person: schoolNum="180xxxxxx" psw="iampassword" email="[email protected]" def __init__(self,schoolNum,psw,email): Self.schoolnum = schoolNum self.psw = PSW self.email = email person.append(Person('schoolnumber','thisispassword','[email protected]')) Person. Append (person ('schoolnumber','password','[email protected]')) # Set headless mode to run in the background because it runs on the Linux command line. chrome_options = Options() chrome_options.add_argument('--no-sandbox') Chrome_options. add_argument('--disable-dev-shm-usage') chrome_options.add_argument('--headless') Because this page if not the mobile end will enter the admin side. Chrome_options. Add_argument (' the user-agent = "Mozilla / 5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X Like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"') # Create object driver = webdriver.Chrome(options=chrome_options) sleep(10) # Enter the website here and fill in the card for the website driver.get( 'http://xgfx.bnuz.edu.cn/xsdtfw/sys/emapfunauth/pages/welcome.do?service=%2Fxsdtfw%2Fsys%2Fswmxsyqxxsjapp%2F*default%2Fi Ndex. Do %3FwxType%3D1') # # Login # I write a lot of sleep, which is to prevent the network speed is too slow, the DOM has not yet been rendered, selenuim to click and so on, can not find the target element, throw an error. Sleep (10) # driver. Click the db certification find_element_by_xpath ('/HTML/body/div/div/div/div/div/button [2] '). Click () sleep (10) # fill in account number here driver.find_elements_by_xpath('//input[@class="ivu-input ivu-input-large ivu-input-with-prefix"]')[0].send_keys( Driver. Find_elements_by_xpath ('//input[@class=" ivU -input ivU -input-large Ivu-input-with-prefix "]')[1].send_keys(per.psw) # click to enter driver.find_element_by_xpath('//button[@class="ivu-btn Ivu-btn-primary ivu-btn-long ivu-btn-large"]'). Click () # Driver.find_element_by_xpath ('//*[@id="app"]/div/div[2]/a[2]').click() sleep(10) # '/ / * [@ id = "app"] / div/div [5] / div/div/div/div/div [7] / div/a/div [2] / div [2] / input'). Send_keys (" 37 ") sleep (10) # click the address in detail If there is a session, do not fill in the # driver.find_element_by_xpath('//*[@id="app"]/div/div[5]/div/div/div/div/div[9]/div/a/div[2]/div[2]/div[1]/textarea').sen D_keys (" no. 18 jinfeng Road" Driver. Find_element_by_xpath (' / / * [@ id = "app"] / div/div [5] / div/div/div/div/div [10] / div/a '). Click () sleep (10) # click no driver.find_element_by_xpath( '//*[@id="app"]/div/div[5]/div/div/div/div/div[10]/div/div/div[1]/div/div/div/a[2]/div[2]/div[1]/label/span[2]').click() # click my family whether contact driver. Find_element_by_xpath (' / / * [@ id = "app"] / div/div [5] / div/div/div/div/div [12] / div/a '). Click () sleep (10) # Click No driver.find_element_by_xpath( '//*[@id="app"]/div/div[5]/div/div/div/div/div[12]/div/div/div[1]/div/div/div/a[2]/div[2]/div[1]/label').click() # Find_element_by_xpath ('//*[@id="app"]/div/div /div/div/div/div/div[14]/div/a').click() sleep(10) # Click No driver.find_element_by_xpath( '//*[@id="app"]/div/div[5]/div/div/div/div/div[14]/div/div/div[1]/div/div/div/a[2]/div[2]/div[1]/label').click() # Driver.find_element_by_xpath ('//*[@id="app"]/div/div /div/div/div/div[16]/div/a').click() sleep(10) # Click No driver.find_element_by_xpath( '//*[@id="app"]/div/div[5]/div/div/div/div/div[16]/div/div/div[1]/div/div/div/a[2]/div[2]/div[1]/label').click() # Click on my family whether isolated driver. Find_element_by_xpath (' / / * [@ id = "app"] / div/div [5] / div/div/div/div/div [18] / div/a '). Click () sleep (# 1) Click no driver. Find_element_by_xpath ( '//*[@id="app"]/div/div[5]/div/div/div/div/div[18]/div/div/div[1]/div/div/div/a[2]/div[2]/div[1]/label').click() Find_element_by_xpath ('//*[@id="app"]/div/div[6]/div/button').click() sleep(10) # Screenshot ('./ch.png') # driver.save_screenshot()Copy the code

Linux Assign scheduled tasks

Once the code is complete, you can place scheduled tasks on the server to run regularly. The crontab is used here. Assume the code name:python_bnuz.py, address:/root/Command line input:vim /etc/crontab

My rule is to execute statements at 0:10 every day:

 python /root/python_bnuz.py
Copy the code

The results show

Clocking status notification:

If you don’t get an email, you haven’t punched in. At this point, you can check the crontab log to see if it is running properly. Check the crontab log statement: tail -f /var/log/cron

conclusion

Thank you for seeing this. This practice was very interesting and I learned a lot of new knowledge that I had not been exposed to before. The joy of coding lies in learning it and applying it to real situations. Automatic clocking has saved me a lot of trouble, and I hope this article helps you too.