selenium自动化测试键盘事件
(1)send_keys(Keys.BACK_SPACE):删除键(BackSpace)
(2)send_keys(Keys.SPACE):空格键(Space)
(3)send_keys(Keys.TAB):制表符(Tab)
(4)send_keys(Keys.ESCAPE):回退键(Esc)
(5)send_keys(Keys.ENTER):回车键(Enter)
(6)send_keys(Keys.CONTROL,'a'):全选(Ctrl+a)
(7)send_keys(Keys.CONTROL,'c'):全选(Ctrl+c)
(8)send_keys(Keys.CONTROL,'x'):全选(Ctrl+x)
(9)send_keys(Keys.CONTROL,'v'):全选(Ctrl+v)
(10)send_keys(Keys.F1):键盘F1
(11)send_keys(Keys.F12):键盘F12
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
# 在输入框输入内容
driver.find_element_by_id('kw').send_keys("selenium")
# 删除多输入的m
driver.find_element_by_id('kw').send_keys(Keys.BACK_SPACE)
# 输入空格键+“教程”
driver.find_element_by_id('kw').send_keys(Keys.SPACE)
driver.find_element_by_id('kw').send_keys("教程")
# 输入组合键Ctrl+a,全选输入框内容
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'a')
# 输入组合键Ctrl+x,剪切输入框内容
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'x')
# 输入组合键Ctrl+v,粘贴输入框内容
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'v')
# 用回车键代替单击操作
driver.find_element_by_id('su').send_keys(Keys.ENTER)
driver.quit()