Keys类提供了键盘上所有的按键方法。send_keys方法用来模拟键盘输入
如下代码:
from time import * from selenium import webdriver #引入Keys模块 from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome() url = "https://www.baidu.com" print(‘new access %s‘ %(url)) driver.get(url) #输入框输入内容 driver.find_element_by_id(‘kw‘).send_keys(‘seleniumm‘) #删除多输入的一个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(‘教程‘) sleep(3) #全选输入框 driver.find_element_by_id(‘kw‘).send_keys(Keys.CONTROL,‘a‘) #剪切输入框 sleep(3) driver.find_element_by_id(‘kw‘).send_keys(Keys.CONTROL,‘x‘) #粘贴输入框 sleep(3) driver.find_element_by_id(‘kw‘).send_keys(Keys.CONTROL,‘v‘) #回车代替click sleep(3) driver.find_element_by_id(‘kw‘).send_keys(Keys.ENTER) sleep(10) driver.quit()
常用键盘操作:
send_keys(Keys.BACK_SPACE)删除键(BackSpace)
send_keys(Keys.SPACE) 空格键
send_keys(Keys_TAB) 制表键
send_keys(Keys_ESCAPE) 回退键
send_keys(Keys_ENTER) 回车键
send_keys(Keys.CONTRAL,‘a‘) 全选(Ctrl+A)
send_keys(Keys.CONTRAL,‘c‘) 复制(Ctrl+C)
send_keys(Keys.CONTRAL,‘x‘) 剪切(Ctrl+X)
send_keys(Keys.CONTRAL,‘v‘) 粘贴(Ctrl+V)
send_keys(Keys.F1) 键盘F1
....
send_keys(Keys.F12) 键盘F12
时间: 2024-12-22 14:42:05