常用方法
删除键:send_keys(Keys.BACK_SPACE)
空格键:send_keys(Keys.SPACE)
制表键:send_keys(Keys.TAB)
回退键:send_keys(Keys.ESCAPE)
回车键:send_keys(Keys.ENTER)
全选:send_keys(Keys.CONTROL,“a”)
复制:send_keys(Keys.CONTROL,“c”)
粘贴:send_keys(Keys.CONTROL,“v”)
剪切:send_keys(Keys.CONTROL,“x”)
F1:send_keys(Keys.F1)
实现代码
"""
键盘操作
"""from selenium import webdriver
from selenium.webdriver.common.by import By
# 调用keys模块from selenium.webdriver.common.keys import Keys
import time
url ="https://www.baidu.com"
driver = webdriver.Chrome()
driver.get(url)# 在输入框输入内容
driver.find_element(By.ID,"kw").send_keys("模拟键盘操作做")
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("教程")
time.sleep(3)# 全选
driver.find_element(By.ID,"kw").send_keys(Keys.CONTROL,'a')
time.sleep(3)# 剪切
driver.find_element(By.ID,"kw").send_keys(Keys.CONTROL,'x')
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()
版权归原作者 南宫柒 所有, 如有侵权,请联系我们删除。