selenium-模拟键盘事件(回车、删除、刷新等)
2020-09-01 本文已影响0人
忘了呼吸的那只猫
sendkeys可以模拟键盘输入,也可以用来模拟键盘上的按键,甚至是组合键
键值 | 解释 |
---|---|
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 |
from selenium import webdriver
from [selenium.webdriver.common.by](http://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](https://www.baidu.com/)”)
el = driver.find_element([By.ID](http://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()