强烈推荐DrissionPage这个库,比selenium快了不止一倍。语法还简单,最关键的是,文档是纯中文的
今天只写一个,用DrissionPage过滑动验证码
首先,找到对应的元素的位置
ele = page.ele('@aria-label=滑块')
等待元素可点击
ele.wait.clickable()
使用该模块的动作链功能去模拟鼠标操作
page.actions.move_to(ele, duration=0.5).hold().move(200, random.uniform(-5, 5), duration=0.1).move(100, random.uniform(-5, 5), duration=0.5).release()
move_to 鼠标移动到指定元素位置
hold 点住不放
move 移动相对位置
release 松开鼠标
需要注意的是,严格一些的验证码会检测鼠标速度和轨迹,如果速度完全匀速,轨迹是一条直线是过不了的,所以在这里一个300像素的滑动条我们把前面200 使用鼠标速度0.1,也就是让鼠标稍微快一些,后面100用0.5让鼠标速度慢一些,上下滑动增加一个随机数,模拟人手抖动和速度。提高成功率。完整代码
page.get(f'https://dtpro.taopiaopiao.com/app/ykfc-beacon-h5/app-main/all-user-profiles/want-see.html?dataId=1446164', timeout=10)
# page.change_mode() # 切换模式
# print(page.html)
# page.wait(5)
page.wait.eles_loaded('@aria-label=滑块')
ele = page.ele('@aria-label=滑块')
if ele:
ele.wait.clickable()
page.actions.move_to(ele, duration=0.5).hold().move(200, random.uniform(-5, 5), duration=0.1).move(100, random.uniform(-5, 5), duration=0.5).release()
# print(ele_2.rect.size,ele_2.rect.midpoint)
page.wait(10)