zoukankan      html  css  js  c++  java
  • Web自动化测试 七 ----- 鼠标、键盘操作

    一、鼠标操作

    在webdriver中,鼠标的一些操作如:双击、右击、悬停、拖动等都被封装在ActionChains类中,我们只用在需要使用的时候,导入这个类就可以了。

    0.ActionChains类提供的鼠标常用方法:

    • perform():执行所有 ActionChains 中存储的行为。
    • context_click():右击
    • double_click():双击
    • drag_and_drop():拖到
    • move_to_element():鼠标悬停

    注意:

    • 使用之前需要引入 ActionChains 类。
    from selenium.webdriver.common.action_chains import ActionChains

    鼠标右击实例

    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains  # 引入 ActionChains 类
    
    browser = webdriver.Chrome()
    browser.get('https://www.baidu.com')  
    
    
    # 定位到要右击的元素
    right_click = browser.find_element_by_link_text('新闻')
    
    # 对定位到的元素执行鼠标右键操作
    #ActionChains(driver):调用ActionChains()类,并将浏览器驱动browser作为参数传入
    #context_click(right_click):模拟鼠标双击,需要传入指定元素定位作为参数
    #perform():执行ActionChains()中储存的所有操作,可以看做是执行之前一系列的操作
    try:
        ActionChains(browser).context_click(right_click).perform()
        print('成功右击')
    except Exception as e:
        print('fail')
    #输出内容:成功双击

    注意:

    • ActionChains(driver):调用ActionChains()类,并将浏览器驱动browser作为参数传入
    • context_click(right_click):模拟鼠标双击,需要传入指定元素定位作为参数
    • perform():执行ActionChains()中储存的所有操作,可以看做是执行之前一系列的操作

    1.鼠标右击

    • context_click():右击
    #   鼠标右击
    # 定位到要右击的元素
    right_click  = browser.find_element_by_id("xx")
    
    # 对定位到的元素执行右击操作
    ActionChains(browser).move_to_element(right_click ).perform()

    2.鼠标双击

    • double_click():双击
    # 定位到要右击的元素
    double_click = browser.find_element_by_id('xx')
    
    # 对定位到的元素执行鼠标右键操作
    ActionChains(browser).context_click(double_click).perform()

    3.鼠标拖动

    • drag_and_drop(source,target):拖动
    • source:开始位置;需要拖动的元素
    • target:结束位置;拖到后需要放置的目的地元素
    # 开始位置:定位到元素的原位置
    source = driver.find_element_by_id("xx")
    
    # 结束位置:定位到元素要移动到的目标位置
    target = driver.find_element_by_id("xx")
    
    # 执行元素的拖放操作
    ActionChains(driver).drag_and_drop(source,target).perform()

    4.鼠标悬停

    • move_to_element():鼠标悬停
    # 定位到要悬停的元素
    move = driver.find_element_by_id("xx")
    
    # 对定位到的元素执行悬停操作
    ActionChains(driver).move_to_element(move).perform()

      

    二、键盘操作

    常用的键盘操作:

    • send_keys(Keys.BACK_SPACE):删除键(BackSpace)
    • send_keys(Keys.SPACE):空格键(Space)
    • send_keys(Keys.TAB):制表键(TAB)
    • send_keys(Keys.ESCAPE):回退键(ESCAPE)
    • send_keys(Keys.ENTER):回车键(ENTER)
    • send_keys(Keys.CONTROL,'a'):全选(Ctrl+A)
    • send_keys(Keys.CONTROL,'c'):复制(Ctrl+C)
    • send_keys(Keys.CONTROL,'x'):剪切(Ctrl+X)
    • send_keys(Keys.CONTROL,'v'):粘贴(Ctrl+V)
    • send_keys(Keys.F1):键盘F1
    • .....
    • send_keys(Keys.F12):键盘F12
    import time
    from selenium.webdriver import Chrome, ActionChains
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = Chrome()
    
    driver.get('http://www.baidu.com')
    
    def wait_click_element(driver, locator):
        wait = WebDriverWait(driver, 20)
        return wait.until(EC.element_to_be_clickable(locator))
    
    e = driver.find_element_by_id('kw')
    
    e.send_keys('ningmengban')
    e.send_keys(Keys.CONTROL, 'a')
    
    time.sleep(2)
    

     

  • 相关阅读:
    004-linux常用命令-文件搜索命令
    004-linux常用命令-权限管理命令
    004-linux常用命令-文件处理命令
    003-linux使用注意事项
    002-linux 基本网络配置
    002-keras简单应用
    001-keras简介
    006-深度学习与NLP简单应用
    论文笔记:(TOG2019)DGCNN : Dynamic Graph CNN for Learning on Point Clouds
    论文笔记:(2019CVPR)PointConv: Deep Convolutional Networks on 3D Point Clouds
  • 原文地址:https://www.cnblogs.com/qyh0902/p/11223273.html
Copyright © 2011-2022 走看看