一.鼠标事件(*鼠标悬停 用的比较多)
使用鼠标事件首先需要先导入:
from selenium.webdriver.common.action_chains import ActionChains
1.鼠标悬停:move_to_element
例:
#coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
time.sleep(1)
#定位元素要移动到的目标位置
set=driver.find_element_by_link_text("设置")
time.sleep(1)
#鼠标悬停
ActionChains(driver).move_to_element(set).perform()
time.sleep(1)
driver.quit()
2.鼠标右击:context_click()
ActionChains(driver).context_click(set).perform()
3.鼠标双击:double_click()
ActionChains(driver).double_click(set).perform()
4.鼠标拖动:drag_and_drop(soure源,target目标)
二.键盘事件
使用键盘事件首先需要先导入:
from selenium.webdriver.common.keys import Keys
删除键(BackSpace): send_keys(Keys.BACK_SPACE) 空格键(Space): send_keys(Keys.SPACE) 制表键(Tab): send_keys(Keys.TAB) 回退键(Esc): send_keys(Keys.ESCAPE) 回车键(Enter): send_keys(Keys.ENTER) 全选 (Ctrl+A): send_keys(Keys.CONTROL,'a') 复制 (Ctrl+C): send_keys(Keys.CONTROL,'c') 剪切 (Ctrl+X): send_keys(Keys.CONTROL,'x') 粘贴 (Ctrl+V): send_keys(Keys.CONTROL,'v') 键盘F1: send_keys(Keys.F1) 键盘F5 (刷新页面): send_keys(Keys.F5) 键盘F12: send_keys(Keys.F12)