一、按键操作
-
通过对监听键盘输入的元素发送键盘按钮指令
-
from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium import webdriver driver = webdriver.Chrome('/Users/qinhaili/Documents/googleDriver/chromedriver') driver.get(url="https://www.baidu.com") driver.maximize_window() get_input = driver.find_element(By.ID,"kw") get_input.send_keys("python",Keys.ENTER)
-
-
常用键盘按键
-
from selenium.webdriver.common.keys import Keys send_keys(Keys.BACK_SPACE) 删除键BackSpace,每次删除1个字符 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.keys import Keys driver = webdriver.Chrome(r"D:\chromeDriver\71\chromedriver71.exe") driver.get(url="https://www.baidu.com") driver.maximize_window() driver.implicitly_wait(3) driver.find_element_by_id('kw').send_keys("python",Keys.ENTER) time.sleep(5) #输入多个删除键,删除多个 # driver.find_element_by_id('kw').send_keys(Keys.BACK_SPACE * 3) #全选Ctrl+a,删除所有 driver.find_element_by_id('kw').send_keys(Keys.CONTROL,'a') driver.find_element_by_id('kw').send_keys(Keys.BACK_SPACE)
-
-
文章来源地址https://www.toymoban.com/news/detail-740873.html
文章来源:https://www.toymoban.com/news/detail-740873.html
到了这里,关于web自动化系列selenium模拟按键操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!