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

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

  • 相关阅读:
    SVN迁移到GIT
    Android之高效率截图
    Android TV 开发(5)
    Android 标题栏(2)
    Android 标题栏(1)
    C# 之抽象类和抽象方法
    浏览器渲染原理及流程
    C#中的DateTime
    由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面。
    配置错误:不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的
  • 原文地址:https://www.cnblogs.com/chengming104/p/8746046.html
Copyright © 2011-2022 走看看