1.鼠标操作事件
from selenium.webdriver.common.action_chains import ActionChains 针对于input标签操作的
ele = driver.find_element_by_name("tj_briicon") # 对定位到的元素执行鼠标悬停的操作 ActionChains(driver).move_to_element(ele).perform() # 鼠标事件有个特殊之处,我们所调用的函数只是注册动作,不会执行 # 如果想要执行,需要在之后加上 perform 函数 # 鼠标悬停到 ele 元素 ActionChains(driver).move_to_element(ele).perform() # 右击 ele 元素 ActionChains(driver).context_click(ele).perform() # 双击ele元素 ActionChains(driver).double_click(ele).perform() # 单击 ele 元素 ActionChains(driver).click(ele).perform() # 拖动元素, 将ele1 拖到 ele2 ActionChains(driver).drag_and_drop(ele1, ele2).perform()
2.键盘操作事件
from selenium import webdriver #依赖是这个类中方法 from selenium.webdriver.common.keys import Keys # 访问网址 driver.get("https://www.baidu.com") # 定位到百度上的文本输入框 ele = driver.find_element_by_id("kw") time.sleep(3) # 输入内容 ele.send_keys("seleniumn") time.sleep(3) # 删除最后一个多余的 n ele.send_keys(Keys.BACK_SPACE) time.sleep(3) # 输入一个空格,然后再输入教程 ele.send_keys(Keys.SPACE) ele.send_keys("教程") # 全选输入框的内容 ele.send_keys(Keys.CONTROL, "a") time.sleep(3) # 剪切选中的内容 ele.send_keys(Keys.CONTROL, "x") time.sleep(3) # 黏贴剪切板里边的内容 ele.send_keys(Keys.CONTROL, "v") time.sleep(3) # 其他的操作内容我们可以看Keys里面的属性
#这个是key类中所有的属性
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'