zoukankan      html  css  js  c++  java
  • 爬虫selenium中动作链接ActionChains

    一.基本语法

    • 生成一个动作actions=ActionChains(driver)
    • 动作添加方法actions.方法
    • 执行 actions.perform()

    二.方法列表

    click(on_element=None) ——单击鼠标左键
    click_and_hold(on_element=None) ——点击鼠标左键,不松开
    context_click(on_element=None) ——点击鼠标右键
    double_click(on_element=None) ——双击鼠标左键
    drag_and_drop(source, target) ——拖拽到某个元素然后松开
    drag_and_drop_by_offset(source, xoffset, yoffset) ——拖拽到某个坐标然后松开
    key_down(value, element=None) ——按下某个键盘上的键
    key_up(value, element=None) ——松开某个键
    move_by_offset(xoffset, yoffset) ——鼠标从当前位置移动到某个坐标
    move_to_element(to_element) ——鼠标移动到某个元素
    move_to_element_with_offset(to_element, xoffset, yoffset) ——移动到距某个元素(左上角坐标)多少距离的位置
    perform() ——执行链中的所有动作
    release(on_element=None) ——在某个元素位置松开鼠标左键
    send_keys(*keys_to_send) ——发送某个键到当前焦点的元素
    send_keys_to_element(element, *keys_to_send) ——发送某个键到指定元素
    

    三.拖动实现(滑块验证会用到移动)

    两种实现方式

    #前面无聊的代码我就略咯
    #方式一:
    actions=ActionChains(driver) #拿到动作链对象
    actions.drag_and_drop(sourse,target).perform() #其中sourse为起始元素对象,target为结束位置元素对象
    #一般用于把一个图片从一个地方拖到另外个元素地方
    
    #方式二:
    actions=ActionChains(driver) #拿到动作链对象
    actions.click_and_hold(移动的对象).perform()
    actions.move_by_offset(x,y).perform()   #x为移动水平距离,y为移动垂直距离,正负代表左右或者上下
    actions.release().perform()  #松开,这一步别忘了
    

    四.类人滑动滑块

    给个段代码哈

    def get_stacks(distance):     #distance为滑动的总长度
        distance += 20
    
        '''
        匀加速减速运行
            v = v0 + a * t
    
        位移:
        s = v * t + 0.5 * a * (t**2)
        '''
    
        # 初速度
        v0 = 0
    
        # 加减速度列表
        a_list = [20,40, 5]
    
        # 时间
        t = 0.1
    
        # 初始位置
        s = 0
    
        # 向前滑动轨迹
        forward_stacks = []
    
        mid = distance * 3 / 5
    
        while s < distance:
            if s < mid:
                a = a_list[random.randint(0, 1)]
    
            else:
                a = -a_list[2]
    
            v = v0
    
            stack = v * t + 0.5 * a * (t ** 2)
    
            # 每次拿到的位移
            stack = round(stack)
    
            s += stack
    
            v0 = v + a * t
    
            forward_stacks.append(stack)
    
        back_stacks = [-1, -1, -2, -3, -2, -3, -2, -2, -3, -1]
    
        return {'forward_stacks': forward_stacks, 'back_stacks': back_stacks}
    
    
    stacks = get_stacks(60)
    print(stacks)
    forward_stacks = stacks['forward_stacks']
    back_stacks = stacks['back_stacks']
    
    slider_button = driver.find_element_by_class_name('滑动东西的类名')
    
    
    ActionChains(driver).click_and_hold(slider_button).perform()
    
    
    for forward_stack in forward_stacks:
        ActionChains(driver).move_by_offset(xoffset=forward_stack, yoffset=0).perform()
    
    for back_stack in back_stacks:
        ActionChains(driver).move_by_offset(xoffset=back_stack, yoffset=0).perform()
    
    time.sleep(0.3)
    ActionChains(driver).move_by_offset(xoffset=5, yoffset=0).perform()  
    time.sleep(0.2)
    ActionChains(driver).move_by_offset(xoffset=-5, yoffset=0).perform()
    time.sleep(0.1)
    ActionChains(driver).move_by_offset(xoffset=1, yoffset=0).perform()  
    time.sleep(0.1)
    ActionChains(driver).move_by_offset(xoffset=-1, yoffset=0).perform()
    
    ActionChains(driver).release().perform()
    
    
  • 相关阅读:
    windwos8.1英文版安装SQL2008 R2中断停止的解决方案
    indwows8.1 英文版64位安装数据库时出现The ENU localization is not supported by this SQL Server media
    Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
    SQL数据附加问题
    eclipse,myeclipse中集合svn的方法
    JAVA SSH 框架介绍
    SSH框架-相关知识点
    SuperMapRealSpace Heading Tilt Roll的理解
    SuperMap iserver manage不能访问本地目的(IE9)
    Myeclipse中js文件中的乱码处理
  • 原文地址:https://www.cnblogs.com/pythonywy/p/11777644.html
Copyright © 2011-2022 走看看