例如按键盘回车键、回退键,通过键盘进行复制、粘贴等操作。在模拟键盘操作之前也需要导入Keys类,代码如下:
from selenium.webdriver.common.keys import Keys
常用的键盘操作:
键盘操作列表常见的键盘操作实现代码如下:
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://cn.bing.com/")
#在搜索框中搜索selenium
driver.find_element_by_id("sb_form_q").send_keys("selenium")
time.sleep(2)
#输入删除键
driver.find_element_by_id("sb_form_q").send_keys(Keys.BACK_SPACE)
time.sleep(2)
#在搜索框中输入pytest
driver.find_element_by_id("sb_form_q").send_keys('pytest')
time.sleep(2)
#输入Ctrl+A(全选操作)
driver.find_element_by_id("sb_form_q").send_keys(Keys.CONTROL,'a')
time.sleep(2)
#输入Ctrl+X(剪切操作)
driver.find_element_by_id("sb_form_q").send_keys(Keys.CONTROL,'x')
time.sleep(2)
#输入Ctrl+V(粘贴操作)
driver.find_element_by_id("sb_form_q").send_keys(Keys.CONTROL,'v')
time.sleep(2)
#输入回车键
driver.find_element_by_id("sb_form_q").send_keys(Keys.ENTER)
time.sleep(2)
driver.close()
网友评论