Selenium works with crontab to realize automatic check-in

Blog post address: click me to view

Cause

A few days ago, when I came into contact with crontab in Linux, I can regularly complete functions such as changing the wallpaper and pushing local files to github. In the past few days, I wondered if I could make an automatic sign-in script. The crawler I used originally took a cookie to log in and sign in, but after a few days the cookie expired and it became cold. By chance, I learned that selenium can simulate browser clicks, so I found an article about automatic check-in to learn, and recorded it at this time.

Introduction to selenium

Selenium is a comprehensive project that provides various tools and dependency packages for the automation of web browsers. He has many functions. This time, mainly combined with Python, the function can be understood as a browser that can simulate a browser and perform various clicks, fill in account passwords and other operations.

Put a screenshot

Insert picture description here

Key points

The main thing is to select the appropriate elements and pretend that the code is a person. You have to open the webpage first, then enter the account number, and then you have to click the login button. For the code, it has to find the login button before clicking and other operations. Therefore, the right and accurate search is very important.

Find element

Pass id

driver.find_element_by_id('loginForm')

By name

driver.find_element_by_name('username')

Via Xpath

#绝对定位 (页面结构轻微调整就会被破坏)
driver.find_element_by_xpath("/html/body/form[1]")
#HTML页面中的第一个form元素
driver.find_element_by_xpath("//form[1]")
#包含 id 属性并且其值为 loginForm 的form元素
driver.find_element_by_xpath("//form[@id='loginForm']")

For search, you can select an element by F12 and right-click and select Copy to have options such as copy xpath

Other points

Click action

driver.find_element_by_xpath('/html/body/div[2]/button').click()

Fill in the information

driver.find_element_by_id('email').send_keys(username)

Get element content

driver.find_element_by_xpath('//*[@id="swal2-content"]').text

Disadvantage

For selenium, I personally feel that the biggest disadvantage is that it is slower than the crawler speed. Although the code uses operations such as disabling pictures and setting timeouts, it is still relatively slow. However, for automatic check-in, it doesn't matter much if it is slow, as long as it can be stable.

Code

"""
功能:自动签到脚本
配置:登录地址 + 账号 + 密码
"""
import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options

def checkin(login_url,username,password):
    chrome_options = Options()  #解决使用chrome报错
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument("enable-automation")
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--window-size=1920,1080")
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--disable-extensions")
    chrome_options.add_argument("--dns-prefetch-disable")
    chrome_options.add_argument("--disable-gpu")

    chrome_prefs = {
    
    }   #禁止加载图片以提高速度
    chrome_options.experimental_options["prefs"] = chrome_prefs
    chrome_prefs["profile.default_content_settings"] = {
    
    "images": 2}
    chrome_prefs["profile.managed_default_content_settings"] = {
    
    "images": 2}
    driver = webdriver.Chrome(options=chrome_options, executable_path='/usr/bin/chromedriver')  # 初始化chrome
    driver.set_page_load_timeout(30) #设置超时以提高速度
    driver.maximize_window()  # 最大化窗口

    try:
        driver.get(login_url)  # 进入登录页面
    except:
        driver.execute_script("window.stop()") #加载超时停止加载执行下一步操作

    print("当前页面为:"+driver.find_element_by_xpath('//*[@id="app"]/section/div/div[1]/div/h4/span').text)
    print("当前账号为:"+username)
    print("--------------------------------")

    try:
        time.sleep(3)  # 延时加载
        driver.find_element_by_id('email').send_keys(username)  # 填充用户名和密码
        driver.find_element_by_id('password').send_keys(password)
        driver.find_element_by_xpath('/html/body/div[1]/section/div/div[1]/div/form/div[4]/button').click()  # 登录
        time.sleep(3)
        driver.find_element_by_xpath('/html/body/div[2]/div/div/div[3]/button').click()   #点击刚进主页弹出的弹窗
        time.sleep(3) #等待两秒,点击read后网页流量是动态增加的
        try:  # 未签到
            driver.find_element_by_xpath("/html/body/div[1]/div/div[3]/section/div[1]/div/div/a").click()  # 点击签到
            print(driver.find_element_by_xpath('//*[@id="swal2-content"]').text)
            driver.find_element_by_xpath("/html/body/div[7]/div/div[3]/button[1]").click()
            print("签到成功,恭喜你,幸运的boy")
        except Exception as e:
            print("已经签到过了")
    except Exception as e:
        print(e)
        print("签到失败")

    time.sleep(3)
    print("当前流量为:"+driver.find_element_by_xpath("//*[@id='app']/div/div[3]/section/div[3]/div[2]/div/div[2]/div[2]/span").text+"G")
    print()
    driver.quit()

checkin("url","账号","密码")
checkin("url","账号","密码")

Write at the end

The above is only a personal summary, I am just new to contact, if there is an error, please contact me to correct it. I will share more interesting things about linux and python in the future.

Guess you like

Origin blog.csdn.net/zss192/article/details/105574863