selenium 鼠标事件操作
1.操作鼠标事件的类:ActionChains
perform() 执行所有ActionChains中存储的行为
context_click() 右击
double_click() 双击
drag_and_drop() 拖动
move_to_element() 鼠标悬停
2.鼠标右击示例
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import time driver = webdriver.Firefox() driver.get("http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fwebcloud.kuaibo.com%2F") #登陆快播私有云 driver.find_element_by_id("user_name").send_keys("username") driver.find_element_by_id("user_pwd").send_keys("123456") driver.find_element_by_id("dl_an_submit").click() time.sleep(3) #定位到要右击的元素 qqq =driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[3]/table/tbody/tr/td[2]") #对定位到的元素执行鼠标右键操作 ActionChains(driver).context_click(qqq).perform()
3.拖动鼠标的操作
#定位元素的原位置 element = driver.find_element_by_name("source") #定位元素要移动到的目标位置 target = driver.find_element_by_name("target") #执行元素的移动操作 ActionChains(driver).drag_and_drop(element, target).perform()