0.导入键盘类Keys()
- selenium中的Keys()类提供了大部分的键盘操作方法;通过send_keys()方法来模拟键盘上的按键。
# 导入键盘类 Keys()
from selenium.webdriver.common.keys import Keys
1.常用的键盘操作:
- send_keys(Keys.BACK_SPACE):删除键(BackSpace)
- send_keys(Keys.SPACE):空格键(Space)
- send_keys(Keys.TAB):制表键(TAB)
- send_keys(Keys.ESCAPE):回退键(ESCAPE)
- send_keys(Keys.ENTER):回车键(ENTER)
- send_keys(Keys.CONTROL,'a'):全选(Ctrl+A)
- send_keys(Keys.CONTROL,'c'):复制(Ctrl+C)
- send_keys(Keys.CONTROL,'x'):剪切(Ctrl+X)
- send_keys(Keys.CONTROL,'v'):粘贴(Ctrl+V)
- send_keys(Keys.F1):键盘F1
- .....
- send_keys(Keys.F12):键盘F12
2.实例
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")
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("教程")
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'v')
driver.find_element_by_id("kw").send_keys(Keys.ENTER)