一、模拟键盘事件
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
url = "http://www.baidu.com" bf = webdriver.Chrome() bf.maximize_window() bf.get(url) bf.find_element_by_css_selector("#kw").send_keys("python")#定位百度输入框输入python time.sleep(1)
#单个键使用 bf.find_element_by_css_selector("#kw").send_keys(Keys.BACKSPACE)#删除一个N
三、模拟组合键输入
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time
url = "http://www.baidu.com" bf = webdriver.Chrome() bf.maximize_window() bf.get(url) bf.find_element_by_css_selector("#kw").send_keys("python")#定位百度输入框输入python time.sleep(1) #全选并复制输入框中的内容 bf.find_element_by_css_selector("#kw").send_keys(Keys.CONTROL,'a') bf.find_element_by_css_selector("#kw").send_keys(Keys.CONTROL,'c') time.sleep(1)
#清空输入框中的内容 bf.find_element_by_css_selector("#kw").clear() time.sleep(1)
#粘贴刚刚复制的内容 bf.find_element_by_css_selector("#kw").send_keys(Keys.CONTROL,'v')