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

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

  • 相关阅读:
    关于NetBeans IDE的配置优化
    浅谈jquery关于select框的取值和赋值
    实验一 操作系统模仿cmd
    0320 《构建之法》前三章读后感
    0318 复利算法4.0
    0318 自动关机
    0317 复利计算总结
    0316 复利计算3.0
    0311 了解和熟悉操作系统实验
    0311 复利计算2.0
  • 原文地址:https://www.cnblogs.com/chengming104/p/8746046.html
Copyright © 2011-2022 走看看