Loading

selenium.模拟键盘操作(Keys)

Keys源码:

    NULL = '\ue000'
    CANCEL = '\ue001'            # ^break
    HELP = '\ue002'
    BACKSPACE = '\ue003'
    BACK_SPACE = BACKSPACE       #删除键
    TAB = '\ue004'               #TAB键
    CLEAR = '\ue005'  
    RETURN = '\ue006'
    ENTER = '\ue007'             #回车键
    SHIFT = '\ue008'             #Shift键
    LEFT_SHIFT = SHIFT
    CONTROL = '\ue009'
    LEFT_CONTROL = CONTROL       #Ctrl 键
    ALT = '\ue00a'               #Alt 键
    LEFT_ALT = ALT
    PAUSE = '\ue00b'
    ESCAPE = '\ue00c'            #ECS键
    SPACE = '\ue00d'             #空格键 
    PAGE_UP = '\ue00e'           #PgUp 键
    PAGE_DOWN = '\ue00f'         #PgDwon 键
    END = '\ue010'               #END 键
    HOME = '\ue011'              #HOME 键
    LEFT = '\ue012'              #左键
    ARROW_LEFT = LEFT  
    UP = '\ue013'                #上键
    ARROW_UP = UP   
    RIGHT = '\ue014'
    ARROW_RIGHT = RIGHT          #右键
    DOWN = '\ue015'              #下键
    ARROW_DOWN = DOWN  
    INSERT = '\ue016'            #insert键 
    DELETE = '\ue017'            #del键

    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'

组合键:

send_keys(Keys.BACK_SPACE)   删除键(BackSpace) 
send_keys(Keys.SPACE)        空格键(Space) 
send_keys(Keys.TAB)          制表键(Tab) 
send_keys(Keys.ESCAPE)       回退键(Esc) 
send_keys(Keys.ENTER)        回车键(Enter) 
send_keys(Keys.CONTROL,'a')  全选(Ctrl+A) 
send_keys(Keys.CONTROL,'c')  复制(Ctrl+C)
send_keys(Keys.CONTROL,'v')  粘贴(Ctrl+V)
send_keys(Keys.CONTROL,'x')  剪切(Ctrl+X)

非组合键:

Keys.ENTER           回车键 
Keys.BACK_SPACE      删除键 
Keys.SPACE           空格键
Keys.TAB             制表键 
Keys.ESCAPE          回退键 
Keys.F5              刷新键 

百度首页做个练习,效果如GIF:

 

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()
action = ActionChains(driver)
driver.get('https://www.baidu.com')

mov = driver.find_element_by_link_text('设置')
action.move_to_element(mov).perform()
sleep(1)
action.move_to_element_with_offset(mov, 0, 60).click().perform()

sleep(1)
input01 = driver.find_element_by_xpath('//*[@id="adv_keyword"]')
input02 = driver.find_element_by_xpath('//*[@id="adv-setting-2"]/input')
input03 = driver.find_element_by_xpath('//*[@id="adv-setting-3"]/input')
input04 = driver.find_element_by_xpath('//*[@id="advanced"]/div/form/div/table/tbody/tr[4]/td[2]/input')

input01.send_keys("云想衣裳花想容")
sleep(1)
input01.send_keys(Keys.CONTROL, 'a')
input01.send_keys(Keys.BACK_SPACE)
sleep(1)
input02.send_keys("云想衣裳花想容,春风拂槛露华浓。")
sleep(1)
input03.send_keys(Keys.TAB, "若非群玉山头见,会向瑶台月下逢。")
sleep(1)

driver.quit()

 

posted @ 2019-04-11 21:26  yonugleesin  阅读(3851)  评论(0编辑  收藏  举报