zoukankan      html  css  js  c++  java
  • selenium 鼠标滑动操作验证码

    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()--这里不是按住滑动,不需要释放鼠标

    该操作可用于解决点击类的验证码,比如:

  • 相关阅读:
    Vue3.0版本以上路由跳转控制台报错调整
    vue-cli3.0 项目如何使用sass less
    vue-element-admin项目npm install 安装不成功问题
    封装axios
    element ui判断是否必填添加校验
    element ui上传图片限制尺寸(宽、高、)大小、格式等
    阿里云服务器挂载新数据盘
    GitHub上最火的40个Android开源项目
    设计模式——设计原则
    设计模式——策略模式
  • 原文地址:https://www.cnblogs.com/chengming104/p/8746046.html
Copyright © 2011-2022 走看看