Selenium原理、安装与自动打卡实战

技术 · 2023-06-06

起因是最近再找无损音乐的资源,
但是需要积分才能下载,
虽然可以使用钞能力,
但是每天能自动签到的话就可以节省下来了嘛,
俗话说的好,骑自行车去酒吧,该省省该花花!
所以根据拾起了Python+Selenium,
今天先来个简单的,Iamtxt书源下载站的自动签到。
音乐些许复杂,涉及图形滑块验证,
但是我聪明的小脑瓜想了一想,
用cookie自动登录就可以绕过啦
明天试试!


1. 安装与原理

一、原理

Selenium是一套Web网站程序自动化解决方案
Selenium客户端库 --> 浏览器驱动 --> 控制浏览器

  1. 自动化程序调用Selenium 客户端库函数(比如点击按钮元素)
  2. 客户端库会发送Selenium 命令 给浏览器的驱动程序
  3. 浏览器驱动程序接收到命令后 ,驱动浏览器去执行命令
  4. 浏览器执行命令
  5. 浏览器驱动程序获取命令执行的结果,返回给我们自动化程序
  6. 自动化程序对返回结果进行处理

二、安装

  1. 安装客户端库
    pip install Selenium
  2. 电脑下载浏览器驱动
    1. chrome浏览器
    2. edge浏览器
      放置路径推荐: c:\tools\chromedriver.exe
  3. 测试安装成功
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# 创建 WebDrive 对象,指向edge驱动
wd = webdriver.Chrome(service=Service(r'C:\\tools\\chromedriver.exe'))
# 调用驱动使用 get 方法用浏览器打开baidu
wd.get('https://www.baidu.com')

三、实战:iamtxt自动打卡

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

# 调试代码,运行结束浏览器不关闭
# options = webdriver.ChromeOptions()
# options.add_experimental_option('detach',True)

# 创建 WebDrive 对象,指向edge驱动
wd = webdriver.Chrome(service=Service(r'C:\\tools\\chromedriver.exe'),options=options)

# 打开iamtxt,自动登录打卡
def iamtxt():
    wd.get('https://www.iamtxt.com/e/member/login/log.html')
    wd.find_element(By.ID,'username').send_keys('******')
    wd.find_element(By.ID,'password').send_keys('******')
    wd.find_element(By.NAME,'Submit').click()
    wd.implicitly_wait(10)
    wd.find_element(By.XPATH,'//*[@id="signin"]').click()
iamtxt()
Python Selenium
  1. vendor (作者)  2023-06-06

    Selenium中文文档:https://python-selenium-zh.readthedocs.io/zh_CN/latest/

All Rights Reserved. Login Theme Jasmine by Kent Liao