sendkeys可以模拟键盘输入,也可以用来模拟键盘上的按键,甚至是组合键
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep,ctime
driver = webdriver.Firefox()
driver.get(“https://www.baidu.com”)
el = driver.find_element(By.ID, “kw”)
el.send_keys(“seleniumm”)
el.send_keys(Keys.BACK_SPACE)
el.send_keys(Keys.SPACE)
el.send_keys(“教程”)
el.send_keys(Keys.CONTROL, ‘a’)
el.send_keys(Keys.CONTROL, ‘x’)
el.send_keys(Keys.CONTROL, ‘v’)
el.send_keys(Keys.ENTER)
driver.close()
键值 | 解释 |
---|---|
send_keys(Keys.BACK_SPACE) | 删除键BackSpace |
send_keys(Keys.SPACE) | 空格键Space |
send_keys(Keys.TAB) | 制表键Tab |
send_keys(Keys.ESPACE) | 回退键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 |
问题:如果遇到以下错误,是没有导入Keys导致的
from selenium.webdriver.common.keys import Keys