selenium使用篇_键盘鼠标事件
大家好,我是bug郭,一名双非科班的在校大学生。对C/JAVA、数据结构、Spring系列框架、测试开发、Linux及MySql、算法等领域感兴趣,喜欢将所学知识写成博客记录下来。 希望该文章对你有所帮助!如果有错误请大佬们指正!共同学习交流
作者简介:
- CSDN java领域新星创作者blog.csdn.net/bug…
- 掘金LV3用户 juejin.cn/user/bug…
- 阿里云社区专家博主,星级博主,developer.aliyun.com/bug…
- 华为云云享专家 bbs.huaweicloud.com/bug…
目录
- 键盘事件
- 键盘按键用法
- 键盘组合键用法
- 鼠标事件
键盘事件
键盘按键用法
要使用键盘按键,需要导入
keys
包,引入Keys
模块!
from selenium.webdriver.common.keys import Keys
通过send_keys()
调用按键:
Keys
模块有很多按键!!!
Keys模块下的按键如下:
class Keys(object):
"""
Set of special keys codes.
"""
NULL = '\ue000'
CANCEL = '\ue001' # ^break
HELP = '\ue002'
BACKSPACE = '\ue003'
BACK_SPACE = BACKSPACE
TAB = '\ue004'
CLEAR = '\ue005'
RETURN = '\ue006'
ENTER = '\ue007'
SHIFT = '\ue008'
LEFT_SHIFT = SHIFT
CONTROL = '\ue009'
LEFT_CONTROL = CONTROL
ALT = '\ue00a'
LEFT_ALT = ALT
PAUSE = '\ue00b'
ESCAPE = '\ue00c'
SPACE = '\ue00d'
PAGE_UP = '\ue00e'
PAGE_DOWN = '\ue00f'
END = '\ue010'
HOME = '\ue011'
LEFT = '\ue012'
ARROW_LEFT = LEFT
UP = '\ue013'
ARROW_UP = UP
RIGHT = '\ue014'
ARROW_RIGHT = RIGHT
DOWN = '\ue015'
ARROW_DOWN = DOWN
INSERT = '\ue016'
DELETE = '\ue017'
SEMICOLON = '\ue018'
EQUALS = '\ue019'
NUMPAD0 = '\ue01a' # number pad keys
NUMPAD1 = '\ue01b'
NUMPAD2 = '\ue01c'
NUMPAD3 = '\ue01d'
NUMPAD4 = '\ue01e'
NUMPAD5 = '\ue01f'
NUMPAD6 = '\ue020'
NUMPAD7 = '\ue021'
NUMPAD8 = '\ue022'
NUMPAD9 = '\ue023'
MULTIPLY = '\ue024'
ADD = '\ue025'
SEPARATOR = '\ue026'
SUBTRACT = '\ue027'
DECIMAL = '\ue028'
DIVIDE = '\ue029'
F1 = '\ue031' # function keys
F2 = '\ue032'
F3 = '\ue033'
F4 = '\ue034'
F5 = '\ue035'
F6 = '\ue036'
F7 = '\ue037'
F8 = '\ue038'
F9 = '\ue039'
F10 = '\ue03a'
F11 = '\ue03b'
F12 = '\ue03c'
META = '\ue03d'
COMMAND = '\ue03d'
我们知道了这些按键就可以进行键盘事件!
'''
键盘事件
'''
from selenium import webdriver
import time
# 引入按键模块
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('http://baidu.com/')
driver.find_element_by_id('kw').send_keys('人生苦短,我用python!')
time.sleep(2)
#定位到搜索按钮然后会车
driver.find_element_by_id('su').send_keys(Keys.ENTER)
time.sleep(3)
#定位到输入框进行清除键
driver.find_element_by_id('kw').send_keys(Keys.BACKSPACE)
time.sleep(2)
driver.quit()
键盘组合键用法
键盘组合键用法
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)
示例:
#键盘组合键用法!
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get('http://baidu.com')
driver.find_element_by_id('kw').send_keys('python之父')
time.sleep(3)
# 通过ctrl+a进行全选!
driver.find_element_by_id('kw').send_keys(Keys.CONTROL,'a')
time.sleep(1)
# 通过 ctrl+x进行剪切
driver.find_element_by_id('kw').send_keys(Keys.CONTROL,'x')
time.sleep(3)
driver.quit()
鼠标事件
要使用鼠标事件要先导入鼠标模块
from selenium.webdriver.common.action_chains import ActionChains
语法示例如下:
#鼠标拖动事件
ActionChains(driver).move_to_element(element).perform()
ActionChains(driver) #生成用户的行为,所有行动都存储在actionchains对象中
move_to_element(element) # 操作鼠标到element元素中
perform() # 执行存储的行为
ActionChains类
- click() 左击
- context_click() 右击
- double_click() 双击
- drag_and_drop() 拖动
- move_to_element() 移动
示例:
# 鼠标事件
from selenium import webdriver
import time
# 导入鼠标事件的模块
from selenium.webdriver.common.action_chains import ActionChains
# 导入键盘事件模块
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("http://baidu.com/")
driver.find_element_by_id('kw').send_keys('国足')
time.sleep(2)
# 定位到搜索按钮!
q = driver.find_element_by_id('su')
# 鼠标事件 鼠标双击
ActionChains(driver).double_click(q).perform()
time.sleep(2)
# 定位到搜索框!
s = driver.find_element_by_id('kw')
# 鼠标单击(左击)
ActionChains(driver).click(s).perform()
time.sleep(2)
# 输入2次键盘向下键
s.send_keys(Keys.ARROW_DOWN)
s.send_keys(Keys.ARROW_DOWN)
time.sleep(2)
# 将鼠标移动到搜索按钮
ActionChains(driver).move_to_element(q).perform()
time.sleep(1)
# 鼠标双击
ActionChains(driver).double_click(q).perform()
time.sleep(2)
driver.quit()