Selenium 鼠标事件 操作大全

今天你学习了吗?

今天你学习了吗

一、导入鼠标事件

1.导入语句
想使用selenium中的鼠标事件,首先我们必须导入ActionChains包,需要注意的是包名称ActionChains两个单词首字母需要大写。

from selenium.webdriver.common.action_chains import ActionChains

二、鼠标常用事件

1.鼠标事件 proform()
调用ActionChains类方法时,不会立即执行,而是将所有操作都存放在一个队列里,当调用perform()方法时,队列里的操作会依次执行。可以理解为对鼠标事件的提交操作,所以首先要记住这个方法。

2.鼠标事件

  • click(on_element=None):单击
    on_element:指被点击的元素,如果该参数为none,将单击当前鼠标所在位置

  • double_click(on_element=None):双击
    on_element:只要双击的元素,如果该参数为none,将单击当前鼠标所在位置

  • context_click(on_element=None): 右击

  • on_element:只要双击的元素,如果该参数为none,将单击当前鼠标所在位置

  • drag_and_drop(source, target):鼠标拖动
    Source:鼠标拖动的元素 Target:鼠标释放的目标元素

  • move_to_element(to_element) :鼠标悬停
    to_element:指定元素

  • move_to_t(to_element) :鼠标移动
    to_element:指定元素

  • perform():提交已保存的操作

下面这俩我在暂时没有用到过

  • click_and_hold(on_element=None):按住鼠标左键在元素上,点击并且不释放
    on_element:指要按住鼠标左键的元素,如果该参数为none,将单击当前鼠标所在位置。

  • release(on_element=None) 释放鼠标
    on_element:被鼠标释放的元素

三、Python 代码实现,鼠标常用事件

# coding = utf-8

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
# 导入鼠标事件
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Firefox()
driver.get('https://www.baidu.com')
element_song = driver.find_element_by_id('kw')
element_ci = driver.find_element_by_id('su')
# 首先把浏览器对象放在ActionChains中,然后使用鼠标事件操作,最后提交操作
ActionChains(driver).double_click(element_song).perform()
# 下面的语句不会执行,因为鼠标事件需要调用profrom(),忘记调用了。就不会执行
ActionChains(driver).context_click(element_song)

sleep(3)
driver.quit()

此博客仅为我业余记录文章所用。部分内容源于网络,发布到此,仅供网友阅读参考,如有侵权,请联系我删除或者修改。

在这个世界上有六十亿人,一个人一生大约会遇到两千九百二十万人,两个人相遇的概率是十万分之四,相识的概率是千万分之五。我很想认识你。

我叫宋辞,一个喜欢胡思乱想的互联网从业者(摩友、歌友、驴友、生发友、测试友)。期待与你相识
songe63

猜你喜欢

转载自blog.csdn.net/weixin_45598506/article/details/107826014