from selenium.webdriver.common.keys import Keys #键盘导入类 ------------------------------------------------------------------------------
常用的键盘操作:
- send_keys(Keys.BACK_SPACE):删除键(BackSpace)
- send_keys(Keys.SPACE):空格键(Space)
- send_keys(Keys.TAB):制表键(TAB)
- send_keys(Keys.ESCAPE):回退键(ESCAPE)
- 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 webdriverfrom selenium.webdriver.common.keys import Keys #键盘导入类from time import sleep driver = webdriver.Firefox() # 指定和打开浏览器driver.get(‘http://www.baidu.com‘) driver.find_element_by_id(‘kw‘).send_keys(‘seleniumm‘) #在输入框输入内容sleep(4) driver.find_element_by_id(‘kw‘).send_keys(Keys.BACK_SPACE) #删除键 #删除多输入的一个字sleep(4) driver.find_element_by_id(‘kw‘).click()driver.find_element_by_id(‘kw‘).send_keys(Keys.SPACE) # 输入空格键sleep(4) driver.find_element_by_id(‘kw‘).send_keys(Keys.CONTROL, ‘a‘) #输入Control+a模拟全选driver.find_element_by_id(‘kw‘).send_keys(Keys.CONTROL, ‘c‘) #输入Control+c模拟复制 driver.find_element_by_id(‘kw‘).click() #单击之后鼠标焦点就在文字后面了,不然还在文字上,粘贴就会直接覆盖文字 driver.find_element_by_id(‘kw‘).send_keys(Keys.CONTROL, ‘v‘) #输入Control+v模拟粘贴sleep(8) driver.find_element_by_id(‘kw‘).send_keys(Keys.ENTER) #回车键sleep(5) driver.close()
原文地址:https://www.cnblogs.com/xiaobaibailongma/p/12041742.html
时间: 2024-10-10 14:18:49