美文网首页
Python + Selenium(十三)鼠标操作-链式操作

Python + Selenium(十三)鼠标操作-链式操作

作者: 猫与测试 | 来源:发表于2019-10-28 09:28 被阅读0次

    当你需要执行复杂的操作时,比如将一个元素按住拖动到另一个元素上去,需要移动鼠标然后点击并按下键盘某个按键等等。

    当然,在 Web 页面上,这种操作好像比较少。

    但是,如果遇到了怎么办呢?这就需要用到 ActionChains 这个类啦。

    ActionChains 提供了对动作的链式操作,也就是可以生成一个操作的队列,将复杂的操作过程分解成单个操作,然后组合起来一次性执行。

    这里面主要是鼠标操作,加上一些键盘操作。

    以简单的鼠标移动为例,京东首页上的商品二级分类默认处于隐藏状态,需要将鼠标移动到一级分类上才会显示。


    我们可以模拟鼠标移动,到元素上然后再点击二级菜单,比如我们先点击家用电器,再点击超薄电视
    # 两种写法,一种是直接链式调用,另一种分别调用
    # 直接链式调用
    from selenium.webdriver import ActionChains
    # ... 省略打开过程
    e = driver.find_element_by_link_text("家用电器")
    # 将鼠标悬停在家用电器上,暂停0.1s
    ActionChains(driver).move_to_element(e).pause(0.1).perform()
    
    # 第二种
    action = ActionChains(driver)
    action.move_to_element(e)
    action.pause(0.1)
    action.perform()
    

    无论哪种方式,动作都是按照它们被调用的顺序执行。

    通过 ActionChains 的对象生成操作队列,在没有执行提交 perform() 之前,所有操作只是暂存于队列中,不会实际在页面上操作,需要执行 perform() 时才会实际执行操作。

    除了上面的鼠标悬停 move_to_element 操作外,还有其他的一些操作:

    鼠标悬停

    1. move_by_offset(xoffset, yoffset) 鼠标移动偏移量

    ActionChains(driver).move_by_offset(100, 100).perform()
    

    偏移量: 从当前位置增加和减少的坐标值, 正数为增加,负数为减少。

    从当前鼠标位置向右下移动,假设当前位置为(300,500),则移动到(400, 600)。

    2. move_to_element(to_element)鼠标移动到指定元素

    e = driver.find_element_by_id('su')
    ActionChains(driver).move_to_element(e).perform()
    

    3. move_to_element_with_offset(to_element, xoffset, yoffset) 将鼠标移动到基于元素的偏移量,偏移量基于鼠标左上角的坐标点

    e = driver.find_element_by_id('su')
    ActionChains(driver).move_to_element_with_offset(e, 100, 100).perform()
    

    移动到 id 为 su 的元素右下方。

    鼠标点击

    1. click(on_element=None) 点击指定元素,如果没有指定元素,则点击当前鼠标所在位置

    e = driver.find_element_by_id('su')
    ActionChains(driver).click(e).perform()
    

    2. double_click(on_element=None) 双击元素,如果没有指定元素则在当前鼠标位置双击

    e = driver.find_element_by_id('su')
    ActionChains(driver).double_click(e).perform()
    

    3. context_click(on_element=None) 在元素上点击鼠标右键,如果没有指定则在当前鼠标位置单击右键

    e = driver.find_element_by_id('su')
    ActionChains(driver).context_click(e).perform()
    

    4. click_and_hold(on_element=None) 鼠标按下不松

    e = driver.find_element_by_id('su')
    ActionChains(driver).click_and_hold(e).perform()
    

    注意,此时鼠标一直处于按下状态,直到执行 release() 释放鼠标的操作。

    拖动元素

    这类操作相当于是鼠标按住某个元素,然后移动鼠标,实现对元素的拖动操作。当然前提是你操作的元素要能够支持拖动。

    1. drag_and_drop(source, target) 在 source 元素上按下鼠标左键,并拖动到 target 元素并松开鼠标

    e1 = driver.find_element_by_id('source')
    e2 = driver.find_element_by_id('target')
    ActionChains(driver).drag_and_drop(e1, e2).perform()
    

    按住 e1 元素,拖动到 e2 元素。

    2. drag_and_drop_by_offset(source, xoffset, yoffset) 在source元素上按下鼠标左键,并按偏移量拖动

    e = driver.find_element_by_id('source')
    ActionChains(driver).drag_and_drop_by_offset(e, -100, -100).perform()
    

    从 e 元素的位置向左上角移动。

    键盘操作

    1. send_keys(*keys_to_send)在当前焦点元素发送按键

    ActionChains(driver).send_keys('手机').perform()
    

    2. send_keys_to_element(self, element, *keys_to_send)向指定元素发送按键

    e = driver.find_element_by_id('kw')
    ActionChains(driver).send_keys_to_element(e, '测试').perform()
    

    3. key_down(value, element=None) 按下一个特殊按键
    只能用于 Ctrl, Alt,Shift 键,注意此时按键只是按下并没有松开,用于进行按键组合操作,如 Ctrl+A。

    4. key_up( value, element=None) 释放一个按下的键
    与 key_down() 配套使用,用于释放一个已按下的按键,只能用于 Ctrl, Alt,Shift 键
    如果要发送组合键,要这么写:

    ActionChains(driver).key_down(Keys.CONTROL) \
        .send_keys('c').key_up(Keys.CONTROL).perform()
    

    队列操作

    1. perform() 提交队列中的所有操作
    所有操作都需要通过 perform() 才会实际提交到浏览器。

    2. rest_actions() 清空队列中的操作
    将队列中已存储的操作清空。

    3. pause(seconds) 暂停所有动作
    相当于等待,用于链式操作过程中的等待。

    4. release(on_element=None) 松开按下的鼠标
    如果有鼠标按下的操作,那么需要通过 release() 释放鼠标。

    相关文章

      网友评论

          本文标题:Python + Selenium(十三)鼠标操作-链式操作

          本文链接:https://www.haomeiwen.com/subject/awgomctx.html