---恢复内容开始---
1、需要先导入keys类包
1 from selenium.webdriver.common.keys import Keys
2、常用的键盘操作
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,'x') 剪切(Ctrl+X)
send_keys(Keys.CONTROL,'v') 粘贴(Ctrl+V)
3、练习
1 #coding=utf-8 2 3 from selenium import webdriver 4 5 #引入Keys类包 6 7 from selenium.webdriver.common.keys import Keys 8 import time 9 10 driver = webdriver.Firefox() 11 driver.get("http://www.baidu.com") 12 13 #输入框输入内容 14 driver.find_element_by_id("kw").send_keys("seleniumm") 15 time.sleep(3) 16 17 #删除多输入的m 18 driver.find_element_by_id("kw").send_keys(Keys.BACK_SPACE) 19 time.sleep(3) 20 21 #输入空格键+“教程” 22 driver.find_element_by_id("kw").send_keys(Keys.SPACE) 23 driver.find_element_by_id("kw").send_keys("教程") 24 time.sleep(3) 25 26 #全选输入的内容 27 driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a') 28 time.sleep(3) 29 30 #剪切所有的内容 31 driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x') 32 time.sleep(3) 33 34 #粘贴内容,重新输入 35 driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'v') 36 time.sleep(3) 37 38 #通过回车代替提交完成搜索 39 driver.find_element_by_id("su").send_keys(Keys.ENTER) 40 time.sleep(3) 41 42 #关闭浏览器 43 driver.close()
---恢复内容结束---