在使用webdriver时,有些时候我们需要做一些键盘上特殊键的操作,例如backspace,ctrl,shift等,这个时候就需要用到webdriver.common.keys.Keys方法来进行

>>> from selenium import webdriver
>>> from selenium.webdriver.common.keys import Keys
>>> a = webdriver.Chrome()
>>> a.get('http://www.baidu.com')
>>> a.find_element_by_id('kw').send_keys('just for test') #在搜索框中输入“just for test”
>>> a.find_element_by_id('kw').send_keys(Keys.BACK_SPACE) #在输入框中的文字后面输入一个backspace键
>>> a.find_element_by_id('kw').send_keys('\ue003') #其实Keys.BACK_SPACE返回的内容就是'\ue003',这两句是等价的
>>> help(Keys)      #取一段Keys的help文档,其中有这些方法的返回结果
| BACKSPACE = '\ue003'
|
| BACK_SPACE = '\ue003'
|
| CANCEL = '\ue001'
|
| CLEAR = '\ue005'
|
| COMMAND = '\ue03d'
|
| CONTROL = '\ue009'
|
| DECIMAL = '\ue028'
|
| DELETE = '\ue017'
|
| DIVIDE = '\ue029'
|
| DOWN = '\ue015'
|
| END = '\ue010'
|
| ENTER = '\ue007'
|
| EQUALS = '\ue019'
|
| ESCAPE = '\ue00c'
|
| F1 = '\ue031'
|
| F10 = '\ue03a'
|
| F11 = '\ue03b'
05-28 12:58