1、鼠标事件:
# 每个模拟事件后需加.perform() 才会执行
# context_click() 右击
# double_click() 双击
# drag_and_drop(source, target) 拖动
# move_to_element() 鼠标悬停
# move_to 移动
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
# move_to 移动
# 定位到元素的源位置
ele = driver.find_element_by_link_text('新闻')
# 将鼠标移动到定位的元素上面
ActionChains(driver).move_to_element(ele).perform()
# 鼠标拖放操作:drag_and_drop(source, target)
# source: 鼠标拖动的源元素。
# target: 鼠标释放的目标元素。
# 定位元素的源位置
element = driver.find_element_by_id("xxx")
# 定位元素要移动到的目标位置
target = driver.find_element_by_id("xxx")
# 执行元素的拖放操作
ActionChains(driver).drag_and_drop(element, target).perform()
2、键盘事件
# send_keys(Keys.BACK_SPACE) 删除键(BackSpace)
# send_keys(Keys.SPACE) 空格键(Space)
# send_keys(Keys.TAB) 制表键(Tab)
# send_keys(Keys.ESCAPE) 回退键(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.F5)键盘F5
# …
# send_keys(Keys.F12) 键盘F12
from selenium import webdriver
# 导入Keys 模块
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
# 输入框输入内容
driver.find_element_by_id("kw").send_keys("seleniumm")
# Backspace键(删除输入的内容)
driver.find_element_by_id('kw').send_keys(Keys.BACK_SPACE)
# 如键盘的F5刷新
driver.find_element_by_id('kw').send_keys(Keys.F5)
# 输入空格键+“教程”
driver.find_element_by_id("kw").send_keys(Keys.SPACE)
driver.find_element_by_id("kw").send_keys(u"教程")
# ctrl+a 全选输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'a')
# ctrl+x 剪切输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'x')
# ctrl+v 粘贴内容到输入框
driver.find_element_by_id("kw").send_keys(Keys.CONTROL, 'v')