background

School is out for winter break, and I’m back to punching in every day at home as I did last year. This year, I want to study how to automatically punch in Python. Clocking is actually a web operation, today to share my implementation method.

For convenience, I found a website to demonstrate.

Panjiachen.gitee. IO/Vue-Element…

Environment to prepare

  1. In Google’s browser, type:chrome://versionPress enter to get the native Google Chrome version number

  2. Download the corresponding version of Google browser driver: npm.taobao.org/mirrors/chr…

    Download and decompress it to the script folder of your native Python

    The driver version must be the same as that of the browser. If you use another browser, download the driver of this browser

  3. Download selenuim

    On the command line, type PIP install Selenium

Implementation tools

  • 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) Using XPATH absolute positioning fails when a page has nested subpages

      However, 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 -> right-click on the element you want to operate -> find the corresponding line of code in the source code -> right-click on the line of code -> Copy -> Copy full xpath

The implementation code

from selenium import webdriver
from time import sleep

Create a new Instance of Chrome
driver = webdriver.Chrome()

# Open the target site
driver.get("https://panjiachen.gitee.io/vue-element-admin/#/login")

The purpose of sleeping is to slow things down a bit for viewing, sometimes waiting for elements to render
sleep(1)

Because the site is filled in by default, so you need to clear it before you fill in your account password.

# account
driver.find_element_by_xpath('/html/body/div/div/form/div[2]/div/div/input').clear()
driver.find_element_by_xpath('/html/body/div/div/form/div[2]/div/div/input').send_keys("editor")

# your password
driver.find_element_by_xpath('/html/body/div/div/form/div[3]/div/div/input').clear()
driver.find_element_by_xpath('/html/body/div/div/form/div[3]/div/div/input').send_keys("123456789098765432")

sleep(3)

# Click login
driver.find_element_by_xpath('/html/body/div/div/form/button').click()


Copy the code