selenium学习:鼠标事件

发布时间:2020-06-18 19:21:22 作者:91ctt
来源:网络 阅读:508

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

ActionChains类提供的常用方法:

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

contextclick() 右击

double_click() 双击

drag_and_drop() 拖动

move_to_element() 鼠标悬停

  1. 鼠标右击事件

  下面代码中:from selenium.driver import ActionChains  导入提供鼠标操作的ActionChains类

   ActionChains(driver)调用ActionChains类,将浏览器驱动driver作为参数传入

   context_click(right_click)方法用于模拟鼠标右键操作,在调用时需要指定元素定位

   perform()执行所有ActionChains中的存储行为,对整个操作的提交动作。

2.鼠标悬停
  move_to_element()方法可以模拟鼠标悬停的动作
3.鼠标双击操作
使用double_click函数
4.鼠标拖放操作
drag_and_drop(source,target)在源元素上按住鼠标左键,然互移动到目标机上释放
source:鼠标拖动的源元素
target鼠标释放的目标元素  

from selenium import webdriver
from time import *
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()

url = "https://www.baidu.com"
print('new access %s' %(url))
driver.get(url)
#定位元素


right_click= driver.find_element_by_xpath('//*[@id="u1"]/a[8]')
#鼠标右击
ActionChains(driver).context_click(right_click).perform()

#鼠标悬停
#ActionChains(driver).move_to_element(right_click).perform()

#鼠标双击
ActionChains(driver).double_click(right_click).perform()
sleep(3)


driver.quit()

推荐阅读:
  1. selenium键盘事件和鼠标事件
  2. QT学习小结之鼠标事件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

selenimu sel

上一篇:SGI版本空间配置器

下一篇:程序的写法:方便以后的查看和维护。

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》