python+selenium模拟键盘输入

from selenium.webdriver.common.keys import Keys      #键盘导入类

------------------------------------------------------------------------------

常用的键盘操作:

  • 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

 

-------------------------------------------------------------------------------------------------------------------------

from selenium import webdriver
from selenium.webdriver.common.keys import Keys #键盘导入类
from time import sleep

driver = webdriver.Firefox() # 指定和打开浏览器
driver.get('http://www.baidu.com')


driver.find_element_by_id('kw').send_keys('seleniumm') #在输入框输入内容
sleep(4)


driver.find_element_by_id('kw').send_keys(Keys.BACK_SPACE) #删除键 #删除多输入的一个字
sleep(4)

driver.find_element_by_id('kw').click()
driver.find_element_by_id('kw').send_keys(Keys.SPACE) # 输入空格键
sleep(4)


driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'a') #输入Control+a模拟全选
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'c') #输入Control+c模拟复制

driver.find_element_by_id('kw').click() #单击之后鼠标焦点就在文字后面了,不然还在文字上,粘贴就会直接覆盖文字

driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'v') #输入Control+v模拟粘贴
sleep(8)


driver.find_element_by_id('kw').send_keys(Keys.ENTER) #回车键
sleep(5)


driver.close()
posted @ 2019-12-15 01:39  小白龙白龙马  阅读(7927)  评论(0编辑  收藏  举报