由于不想亲自动手打开某网站登陆然后签到,所以制作一个python脚本实现自动操作。
Selenium官网链接: https://www.selenium.dev/
官网介绍说主要用于自动化web应用程序的测试目的,但当然不限于此。无聊的基于web的管理任务也可以(也应该)自动化。
当然也表示了:What you do with that power is entirely up to you.
如标题所说,我也就想自动签到。
安装
如果这一步出现什么问题去查看官方文档
当前日期(20230828)自动安装的版本是:selenium==4.11.2
代码 网站URL和用户密码就不贴出来了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.chrome.service import Serviceoptions = Options() options.add_argument('--headless' ) service = Service() driver = webdriver.Chrome(options=options, service=service) try : url = 'https://www.xxx.com/login.html' driver.get(url) username_input = driver.find_element(By.ID, 'user' ) password_input = driver.find_element(By.ID, 'password' ) username_input.clear() password_input.clear() username_input.send_keys('username' ) password_input.send_keys('password' ) login_button = driver.find_element(By.LINK_TEXT, '登录' ) login_button.click() qd_url = 'https://www.xxx.com/xx.html' driver.get(qd_url) qd_button =driver.find_element(By.LINK_TEXT, '签到' ) qd_button.click() res_confirm = driver.find_element(By.ID, 'tt-from' ) if res_confirm: print ('DONE>>>>>>' ) else : print ('<<<<<<BAD' ) except Exception as e: print (e) print ('OPS,ERROR>>>>>' ) finally : driver.quit()
注意 使用以下代码,需要本地能访问 https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json 该文件,然后访问 https://edgedl.me.gvt1.com 下载对应的 ChromeDriver
1 2 3 # 4.6版本之后使用如下代码, 当然使用上面代码指定路径也可以运行 service = Service() driver = webdriver.Chrome(options=options, service=service)
如此说来,这种方式需要实时联网操作,对于本地内部使用不是很友好
如本地内部使用,还是去下载对应版本的 ChromeDriver
20230922 补充