模拟键盘

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

#下面是一些常用的键盘事件: 
# Keys.BACK_SPACE:回退键(BackSpace)
# Keys.TAB:制表键(Tab)
# Keys.ENTER:回车键(Enter)
# Keys.SHIFT:大小写转换键(Shift)
# Keys.CONTROL:Control键(Ctrl)
# Keys.ALT:ALT键(Alt)
# Keys.ESCAPE:返回键(Esc)
# Keys.SPACE:空格键(Space)
# Keys.PAGE_UP:翻页键上(Page Up)
# Keys.PAGE_DOWN:翻页键下(Page Down)
# Keys.END:行尾键(End)
# Keys.HOME:行首键(Home)
# Keys.LEFT:方向键左(Left)
# Keys.UP:方向键上(Up)
# Keys.RIGHT:方向键右(Right)
# Keys.DOWN:方向键下(Down)
# Keys.INSERT:插入键(Insert)
# DELETE:删除键(Delete)
# NUMPAD0 ~ NUMPAD9:数字键1-9
# F1 ~ F12:F1 - F12键
# (Keys.CONTROL, ‘a’):组合键Control+a,全选
# (Keys.CONTROL, ‘c’):组合键Control+c,复制
# (Keys.CONTROL, ‘x’):组合键Control+x,剪切
# (Keys.CONTROL, ‘v’):组合键Control+v,粘贴
'''

#模拟键盘上键
def up_key():
    try:
        KeyBoardKeys.one_key('up')
        print("光标向上移动成功")
    except Exception as e:
        print(e)

#模拟键盘下键
def down_key():
    try:
        KeyBoardKeys.one_key('down')
        print("光标向下移动成功")
    except Exception as e:
        print(e)

#模拟左键
def left_key():
    try:
        KeyBoardKeys.one_key('left')
        print("光标向左移动成功")
    except Exception as e:
        print(e)

#模拟右键
def right_key():
    try:
        KeyBoardKeys.one_key('right')
        print("光标向右移动成功")
    except Exception as e:
        print(e)

# 模拟ctrl+v键
def ctrl_v(by,locator,value):
    try:
        get_element(driver, by, locator).click()
        Clipboard.set_text(value)
        KeyBoardKeys.two_keys('ctrl', 'v')
        print("模拟键盘粘贴成功")
    except Exception as e:
        raise e

# 模拟tab键移动网页位置
def tab_key():
    try:
        KeyBoardKeys.one_key('tab')
        print("模拟键盘按tab键成功")
    except Exception as e:
        raise e

# 模拟enter键
def enter_key():
    try:
        KeyBoardKeys.one_key('enter')
        print("模拟键盘按enter键成功")
    except Exception as e:
        raise e
原文地址:https://www.cnblogs.com/xiaochuichui/p/13280892.html