python selenium 常用键盘事件
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) send_keys(Keys.F1) 键盘F1 send_keys(Keys.F12) 键盘F12 |
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
# 输入框输入内容
driver.find_element_by_id('kw').send_keys('seleniumm')
# 删除多输入的一个m
time.sleep(3)
driver.find_element_by_id('kw').send_keys(Keys.BACK_SPACE)
# 输入空格键+“教程”
time.sleep(3)
driver.find_element_by_id('kw').send_keys(Keys.SPACE)
driver.find_element_by_id('kw').send_keys('教程')
# ctrl+a全选输入框内容
time.sleep(3)
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'a')
# ctrl+x剪切输入框内容
time.sleep(3)
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'x')
# ctrl+v粘贴内容到输入框
time.sleep(3)
driver.find_element_by_id('kw').send_keys(Keys.CONTROL, 'v')
# 通过回车键来代替单击操作
time.sleep(3)
driver.find_element_by_id('su').send_keys(Keys.ENTER)
# 退出
time.sleep(3)
driver.quit()