from selenium.webdriver.common.keys import Keys #导入键盘操
语法:
send_keys(Keys.BACK_SPACE) 删除键(BackSpace) send_keys(Keys.TAB) 制表键(Tab) send_keys(Keys.SPACE) 空格键(Space) send_keys(Keys.ESCAPE) 回退键(Esc) send_keys(Keys.ENTER) 回车键(Enter) send_keys(Keys.CONTROL,'a') 全选(Ctrl+A) send_keys(Keys.CONTROL,'c') 复制(Ctrl+C) 注意:字母大小写
实例: from time import sleep #导入时间类 from selenium.webdriver.common.keys import Keys #导入键盘操作类 from selenium import webdriver #导入webdriver模块 driver = webdriver.Firefox() #打开Firefox浏览器 driver.get ('https://www.baidu.com/') ele = driver.find_element_by_css_selector('#kw') ele.send_keys('selenium') ele.send_keys(Keys.CONTROL,'a') sleep(2) ele.send_keys(Keys.BACK_SPACE) sleep(3) ele.send_keys('汇智动力') sleep(3) ele.send_keys(Keys.CONTROL,'a') sleep(3) ele.send_keys(Keys.CONTROL,'c') sleep(3) ele.send_keys(Keys.ENTER) sleep(3) e = driver.find_element_by_css_selector('#kw') sleep(3) e.send_keys(Keys.CONTROL,'a') sleep(3) e.send_keys(Keys.BACK_SPACE) sleep(3) e.send_keys(Keys.CONTROL,'v') sleep(3) e.send_keys(Keys.CONTROL,'v')