说明:主要介绍selenium的操作方法和流程。(使用selenium模拟浏览器操作,可以完成一些比较复杂的操作,也是爬虫的一个大类)
一、selenium自动化操作(点击+输入)
1.流程
1)网站登录(selenium模拟登录)
2)跳转页面(selenium模拟点击)
3)模拟填写和提交表单(selenium模拟填写和提交)
4)关闭提交页面
2.分析
1)登录网站、填写内容都需要进行信息的传递使用selenium进行控制比较方便
2)登录网站之后不必通过点击到达填报页面,也可以使用其页面地址直接跳转,登录之后跳转网站后台已经保存了你的登录session。(最好懂一点点session和cookie的基础知识)
3.主要代码
我自己写的时候是没这么多注释。为了便于大家理解在后期又加上了这些注释。如果还有不理解的地方,或者认为有问题的地方欢迎指正。
主体结构:
- 登录网站
- 跳转填报页面
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
vpn_username = "username"
vpn_password = "password" # 填自己的
class check_in(object):
driver_path = r'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe' # 填自己的,chromedriver.exe一般就放在这个位置中
def __init__(self):
option = webdriver.ChromeOptions()
# 设置不显示页面,隐式执行(测试代码时建议将这样注释掉,可以直观地看到自己在那一个步骤出bug了)
option.add_argument('--headless')
# 设置不加载图片,加快加载速度
prefs = {'profile.managed_default_content_settings.images': 2}
option.add_experimental_option('prefs', prefs)
self.driver = webdriver.Chrome(executable_path=check_in.driver_path,chrome_options=option)
# 设置最大等待时间,避免阻塞
self.driver.set_page_load_timeout(20)
self.driver.set_script_timeout(20)
self.url = 'https://****' # 登录页面的url
self.url_apply= 'https://****' # 填报页面的url
def run(self):
'''
模拟登陆网站
注意这次网站登录是没有验证码的,如果有验证码还需要进行验证码的解析(现在的验证码都比较麻烦)
'''
try:
self.driver.get(self.url)
self.driver.find_element_by_name("username").send_keys(vpn_username) # 填写用户名
self.driver.find_element_by_name("password").send_keys(vpn_password) # 填写密码
self.driver.find_element_by_name("login_submit").click() # 点击登录
except:
print("加载页面太慢,停止加载,继续下一步操作")
self.driver.execute_script("window.stop()") # 这里是防止网页一直加载不能之后后面代码
def tianxie(self):
'''
对,你没看错,这个方法叫做填写,hhh
填写网站中的相关内容
'''
self.driver.execute_script("window.open('%s')" % self.url_apply) # 新打开一个页面
self.driver.switch_to.window(self.driver.window_handles[1]) # 定位新打开的页面
time.sleep(2) # 最好在每个步骤的前面都加上时间间隔,防止页面没打开,自动化和爬虫其实是有一定区别的,我们需要的不是快速爬取信息(仅针对这个例子啊,别杠精)
# 单选,选择寝室校区
select_xiaoqu = Select(self.driver.find_element_by_name("fieldSQxq"))
select_xiaoqu.select_by_value("6")
# 单选,选择寝室楼
select_gongyu = Select(self.driver.find_element_by_name('fieldSQgyl'))
select_gongyu.select_by_value("60")
# 填写寝室号
try:
self.driver.find_element_by_name('fieldSQqsh').clear()
except:
print('寝室号似乎本来就是空的!!')
self.driver.find_element_by_name('fieldSQqsh').send_keys('318')
# 选择个人状态
try:
self.driver.find_element_by_id('V2_CTRL28').click()
self.driver.find_element_by_id('V2_CTRL19').click()
self.driver.find_element_by_id('V2_CTRL23').click()
except:
print("莫得感情,现在还签到不了")
# 提交表单
self.driver.find_element_by_class_name('command_button').click()
time.sleep(1)
self.driver.find_element_by_xpath("//button[@class='dialog_button default fr']").click()
time.sleep(1)
self.driver.find_element_by_xpath("//button[@class='dialog_button default fr']").click()
time.sleep(1)
# 非常重要(关闭全部的页面)
self.driver.quit()
if __name__ == '__main__':
auto_checkin = check_in()
print("正在登陆填写平台...")
auto_checkin.run()
print("登陆成功...\n正在跳转填页面...")
auto_checkin.tianxie()
print("填报完成了!!!")
4.注意(期间可能出现的报错)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated
原因:没有获取到网页页面的信息,表示页面加载出问题了,尝试在出问题的行前加上time.sleep(2)
使其加载一定的时间再执行后续工作- 网页一直加载,后面的selenium代码执行不了,300s之后程序自动kill。
这个网站可能设置了动态加载,使用selenium不能一直等待其加载完成,在加载一段时间之后直接停止加载即可。
self.driver.set_page_load_timeout(20)
self.driver.set_script_timeout(20)
-
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 86
你的chromedriver.exe与你的chrome版本不一致了,去下载一个新的chromedriver.exe将原有的替换掉
地址:http://chromedriver.storage.googleapis.com/index.html
chomedriver.exe放到你电脑中C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe
这个位置上,或者其他位置(没试过) -
网页中元素定位不到(这个是最基础也是非常关键的一步,所以建议多学学相关知识,HTML+CSS+Xpath+正则):
1)使用driver.find_element_by_name之内根据tag知识获取
2)使用css定位获取
3)使用xpath定位获取
二、windows10自动运行程序进行填报
我使用windows10的后台自动运行,也是参考别人的教程:
1)windows 10 设定计划任务自动执行 python 脚本的方法_JJLiu天姿
这个教程基本流程比较齐全了,但是关于最后路径的位置可能有所欠缺,可以从2)中得到补充
2)Windows创建定时任务执行Python脚本_CodingDang
这样就可以进行自动系统填报了,懒人必备。。。请不要多次频繁使用自动登录、点击。这样会增加网页服务器的压力。