webdriver的Keys()类,提供了几乎所有按键的方法,常用的如下:
- Keys.BACK_SPACE 删除键
- Keys.SPACE 空格键
- Keys.TAB 制表键(Tab)
- Keys.ESCAPE 回退键(Esc)
- Keys.ENTER 回车键(Enter)
- Keys.CONTROL Ctrl键
ps:元素的send_keys()方法,可以模拟键盘输入
常用的键盘操作示例:
最终输入结果为:zhangyan ffzhangyan fffrom selenium import webdriver from selenium.webdriver.common.keys import Keys # 导入Keys类 import time driver = webdriver.Chrome() driver.get(‘http://www.baidu.com‘) driver.find_element_by_id(‘kw‘).send_keys(‘zhangyang‘) time.sleep(2) driver.find_element_by_id(‘kw‘).send_keys(Keys.BACK_SPACE) time.sleep(2) driver.find_element_by_id(‘kw‘).send_keys(Keys.SPACE) time.sleep(2) driver.find_element_by_id(‘kw‘).send_keys(‘ff‘) time.sleep(2) driver.find_element_by_id(‘kw‘).send_keys(Keys.CONTROL, ‘a‘) # ctrl+a 全选输入框内容 time.sleep(2) driver.find_element_by_id(‘kw‘).send_keys(Keys.CONTROL, ‘c‘) # ctrl+c 复制输入框内容 time.sleep(2) driver.find_element_by_id(‘kw‘).click() driver.find_element_by_id(‘kw‘).send_keys(Keys.CONTROL, ‘v‘) # ctrl+a 粘贴输入框内容 time.sleep(2) driver.find_element_by_id(‘su‘).send_keys(Keys.ENTER) # 通过回车键来代替单击操作 time.sleep(5) driver.close()
原文地址:https://www.cnblogs.com/xiaochongc/p/12431678.html
时间: 2024-10-21 05:08:19