Selenium原理、安装与自动打卡实战
起因是最近再找无损音乐的资源,
但是需要积分才能下载,
虽然可以使用钞能力,
但是每天能自动签到的话就可以节省下来了嘛,
俗话说的好,骑自行车去酒吧,该省省该花花!
所以根据拾起了Python+Selenium,
今天先来个简单的,Iamtxt书源下载站的自动签到。
音乐些许复杂,涉及图形滑块验证,
用cookie自动登录就可以绕过啦
1. 安装与原理
一、原理
Selenium是一套Web网站程序自动化解决方案
Selenium客户端库 --> 浏览器驱动 --> 控制浏览器
- 自动化程序调用Selenium 客户端库函数(比如点击按钮元素)
- 客户端库会发送Selenium 命令 给浏览器的驱动程序
- 浏览器驱动程序接收到命令后 ,驱动浏览器去执行命令
- 浏览器执行命令
- 浏览器驱动程序获取命令执行的结果,返回给我们自动化程序
- 自动化程序对返回结果进行处理
二、安装
- 安装客户端库
pip install Selenium
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()
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步