selenium 鼠标滑动操作验证码

597 篇文章 17 订阅
51 篇文章 5 订阅

selenium 鼠标点击操作比较简单,这里就不介绍了,主要说一下鼠标滑动(按住并滑动),经常用于解决自动化操作的滑动验证码

下面举个简单的例子,比如这种验证码:
在这里插入图片描述
代码:

div = driver.find_element_by_id("nc_1_n1z")
ActionChains(driver).click_and_hold(on_element=div).perform()
time.sleep(0.15)
ActionChains(driver).move_to_element_with_offset(to_element=div, xoffset=30, yoffset=10).perform()
time.sleep(1)
ActionChains(driver).move_to_element_with_offset(to_element=div, xoffset=100, yoffset=20).perform()
time.sleep(0.5)
ActionChains(driver).move_to_element_with_offset(to_element=div, xoffset=200, yoffset=50).perform()

亲测是可以的,不过为了确保一定过 把一次滑动分为三段,还可以进一步优化,把每次滑动的距离和间隔时间做随机
但是笔者又遇到一个问题,在同一个文件里面 下面的程序还要做一次滑动操作,发现无法滑动,debug跟踪发现,下面的滑动并没有起作用

原因:因为上面的操作 没有释放鼠标,那么如何释放鼠标呢?官方只说了 release()方法,具体如何使用,看代码

div = driver.find_element_by_id("nc_1_n1z")
ActionChains(driver).click_and_hold(on_element=div).perform()
time.sleep(0.15)
ActionChains(driver).move_to_element_with_offset(to_element=div, xoffset=30, yoffset=10).perform()
time.sleep(1)
ActionChains(driver).move_to_element_with_offset(to_element=div, xoffset=100, yoffset=20).perform()
time.sleep(0.5)
ActionChains(driver).move_to_element_with_offset(to_element=div, xoffset=200, yoffset=50).release().perform()

在这里插入图片描述
PS:在做鼠标滑动操作时,都是在这个地方写操作函数

比如你要根据相对某个元素偏移坐标处做点击操作

ActionChains(driver).move_to_element_with_offset(to_element=div, xoffset=200, yoffset=50).click().perform()–这里不是按住滑动,不需要释放鼠标

该操作可用于解决点击类的验证码,比如:
在这里插入图片描述看完点赞 ~养成好习惯,以上内容希望对你有帮助,如果对软件测试、接口测试、自动化测试、面试经验交流感兴趣可以加入我们。642830685,免费领取最新软件测试大厂面试资料和Python自动化、接口、框架搭建学习资料!技术大牛解惑答疑,同行一起交流。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值