• web自动化测试第7步:模拟键盘事件(Keys)


    对于模拟键盘事件,我们之前的课程中已经有了一些了解,比如基础操作send_keys()方法输入任意值、鼠标事件key_down()、key_up()等,这一节呢,我们也比较简单,我们专门来讲一讲如何使用引入的common.keys库

    1.基于send_keys()基础上的key

    driver.find_element_by_id("kw").send_keys(Keys.BACK_SPACE)

    如上所示,在基于send_keys()方法的基础上,输入Keys类下封装的方法

    下面是keys库的源码

    from __future__ import unicode_literals
    
    
    class Keys(object):
        """
        Set of special keys codes.
        """
    
        NULL = 'ue000'
        CANCEL = 'ue001'  # ^break
        HELP = 'ue002'
        BACKSPACE = 'ue003'
        BACK_SPACE = BACKSPACE
        TAB = 'ue004'
        CLEAR = 'ue005'
        RETURN = 'ue006'
        ENTER = 'ue007'
        SHIFT = 'ue008'
        LEFT_SHIFT = SHIFT
        CONTROL = 'ue009'
        LEFT_CONTROL = CONTROL
        ALT = 'ue00a'
        LEFT_ALT = ALT
        PAUSE = 'ue00b'
        ESCAPE = 'ue00c'
        SPACE = 'ue00d'
        PAGE_UP = 'ue00e'
        PAGE_DOWN = 'ue00f'
        END = 'ue010'
        HOME = 'ue011'
        LEFT = 'ue012'
        ARROW_LEFT = LEFT
        UP = 'ue013'
        ARROW_UP = UP
        RIGHT = 'ue014'
        ARROW_RIGHT = RIGHT
        DOWN = 'ue015'
        ARROW_DOWN = DOWN
        INSERT = 'ue016'
        DELETE = 'ue017'
        SEMICOLON = 'ue018'
        EQUALS = 'ue019'
    
        NUMPAD0 = 'ue01a'  # number pad keys
        NUMPAD1 = 'ue01b'
        NUMPAD2 = 'ue01c'
        NUMPAD3 = 'ue01d'
        NUMPAD4 = 'ue01e'
        NUMPAD5 = 'ue01f'
        NUMPAD6 = 'ue020'
        NUMPAD7 = 'ue021'
        NUMPAD8 = 'ue022'
        NUMPAD9 = 'ue023'
        MULTIPLY = 'ue024'
        ADD = 'ue025'
        SEPARATOR = 'ue026'
        SUBTRACT = 'ue027'
        DECIMAL = 'ue028'
        DIVIDE = 'ue029'
    
        F1 = 'ue031'  # function  keys
        F2 = 'ue032'
        F3 = 'ue033'
        F4 = 'ue034'
        F5 = 'ue035'
        F6 = 'ue036'
        F7 = 'ue037'
        F8 = 'ue038'
        F9 = 'ue039'
        F10 = 'ue03a'
        F11 = 'ue03b'
        F12 = 'ue03c'
    
        META = 'ue03d'
        COMMAND = 'ue03d'


    2.百度注册输入框,keys示例

    from selenium import  webdriver
    from selenium.webdriver.common.keys import Keys
    from time import sleep
    
    # 模拟键盘操作-百度登录页面
    driver = webdriver.Chrome()
    driver.get("https://passport.baidu.com/v2/?reg")
    
    # 用户名元素定位和手机号元素定位
    name_element = driver.find_element_by_css_selector("#TANGRAM__PSP_3__userName")
    phone_element = driver.find_element_by_css_selector("#TANGRAM__PSP_3__phone")
    
    # 在手机号输入框输入数据:test
    phone_element.send_keys("test")
    
    # 在手机号输入框进行组合键Ctrl+a 、 Ctrl+x
    phone_element.send_keys(Keys.CONTROL, "a")
    sleep(2)
    phone_element.send_keys(Keys.CONTROL, "x")
    
    # 在名字输入框输入组合键Ctrl+v
    name_element.send_keys(Keys.CONTROL, "v")
    
    # 在名字输入框输入数字键3
    name_element.send_keys(Keys.NUMPAD3)


  • 相关阅读:
    Linux- 关于windows和Linux和Mac的换行符
    HIVE- 大数据运维之hive管理
    MySQL- SQL UNION 和 UNION ALL 操作符
    【loj3044】【zjoi2019】Minimax
    【loj3043】【zjoi2019】线段树
    【uoj336】【清华集训2017】无限之环
    【cf contest 1119 F】Niyaz and Small Degrees
    【学习笔记 边分树】【uoj400】【CTSC2018】暴力写挂
    【纪中集训2019.3.25】礼物
    【纪中集训2019.3.25】芬威克树
  • 原文地址:https://www.cnblogs.com/CCGGAAG/p/7800331.html
Copyright © 2020-2023  润新知