需要引入 Keys 类,模拟键盘的操作
示例:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
driver = webdriver.Chrome()
driver.get("
https://www.baidu.com")
driver.maximize_window()
sleep(2)
driver.find_element_by_css_selector("#kw").send_keys("test1")
# 键盘全选操作 Ctrl+A
driver.find_element_by_css_selector("#kw").send_keys(Keys.CONTROL,'a')
sleep(2)
# 键盘剪切操作 Ctrl+x
driver.find_element_by_css_selector("#kw").send_keys(Keys.CONTROL,'x')
sleep(2)
driver.get("
https://www.sogou.com")
driver.maximize_window()
sleep(2)
# 键盘粘贴操作 Ctrl+V
driver.find_element_by_css_selector("#query").send_keys(Keys.CONTROL,'v')
sleep(2)
driver.find_element_by_css_selector("#stb").click
sleep(2)
driver.quit()