Python + selenium realizes automatic campus web page login (Firefox)

Purpose

通过分析页面,实现校园网自动输入账号密码,点击登录的操作

Environment deployment

  1. win10 Firefox 87 (64位) python 3.9

  2. install selenium

pip install selenium

  1. Install driver for firefox
    Firefox driver download version address:
    https://github.com/mozilla/geckodriver/releases

analysis page

  1. Find the username input element of the HTML page and analyze the logic.
    image

  2. Look for password input elements
    image

  3. Analyze the js logic of the page
    and find that there is a js evento display the hidden password box

    image

    So selenium needs to implement the logic code requirements to click on the previous element to trigger the display #pwd element

implement logic code

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium import webdriver
from time import sleep

driver= webdriver.Firefox()


driver.get("http://10.8.2.2")
driver.set_window_size(895, 739)
# 方案一 利用tab自动输入密码后跳转到密码框
# driver.find_element(By.ID, "username").send_keys("user", Keys.TAB,"passwd")


# 方案二 分析代码原理,点击元素后触发原来的js,显示出密码框,之前是隐藏的
driver.find_element(By.ID, "username").send_keys("user")
driver.find_element(By.ID, "pwd_tip").click()
driver.find_element(By.ID, "pwd").send_keys("passwd")
driver.find_element(By.ID, "selectDisname").click()
driver.find_element(By.ID, "_service_2").click()
driver.find_element(By.ID, "loginLink_div").click()

demo video

Portal

Notice

This article is only published in the blog garden Billyme and CSDN horizen08 , and reprinting is not allowed. If you see this article anywhere, it is purely malicious misappropriation.

Guess you like

Origin blog.csdn.net/horizon08/article/details/115917185