鼠标事件

在WebDriver中,鼠标操作的方法封装在ActionChains类中

ActionChains类提供了鼠标操作的常用方法:

perform()                        执行所有ActionChains中存储的行为

context_click()                右击

double_click()                双击

move_to_element()        悬停

drag_and_drop()              拖动

例子:

鼠标事件_第1张图片

Python脚本:

from selenium import webdriver

from selenium.webdriver.common.action_chains import ActionChains

dr = webdriver.Firefox()

url = "http://www.baidu.com"

dr.get(url)

#鼠标悬停

move_to = dr.find_element_by_xpath("//*[@id='u1']/a[8]")

ActionChains(dr).move_to_element(move_to).perform()

from selenium.webdriver.common.action_chains import ActionChains

导入提供鼠标操作的ActionChains类

ActionChains(dr)

调用ActionChains类并将浏览器驱动dr作为参数传入

move_to_element(move_to)

move_to_element()方法用于模拟鼠标悬停操作,在调用时需要指定元素的定位,move_to即定位元素

perform()

执行所有ActionChains中存储的行为,可以理解成对整个操作的提交动作


drag_and_drop(source,target)在源元素上按住鼠标左键,然后移动到目标元素上释放

#......

#定位元素的原始位置

source = dr.find_element_by_id("xx")

#定位元素要移动到的目标位置

target = dr.find_element_by_id("yy")

#执行元素的拖放操作

ActionChains(dr).drag_and_drop(source,target).perform()

#......

你可能感兴趣的:(鼠标事件)