zoukankan      html  css  js  c++  java
  • 【selenium学习 -5】selenium的鼠标操作

    在使用之前需要导入ActionChains模块

    from selenium.webdriver.common.action_chains import ActionChains

    先看一个例子,在百度中搜索 hahah,然后将鼠标移动到 设置 按钮上,并点击菜单中的 搜索设置

    from selenium.webdriver.common.action_chains import ActionChains
    from selenium import webdriver
    import time
    
    if __name__ == '__main__':
        driver = webdriver.Chrome()
        driver.get('https://www.baidu.com/')
        driver.maximize_window()  # 最大化窗口
        # 输入关键字hahah
        driver.find_element_by_id('kw').send_keys('hahah')
        driver.find_element_by_id('su').click()
        time.sleep(3)
    
        # 移动到 设置
        ele= driver.find_element_by_name('tj_settingicon')
        ActionChains(driver).move_to_element(ele).perform()
    
        # 单击,菜单中的"搜索设置"
        driver.find_element_by_link_text('搜索设置').click()

    需要注意的是:

    1.ActionChains实例化时,参数为当前的driver

    2.每个鼠标的操作方法后面都需要跟上perform()

    鼠标点击:

    ActionChains(driver).click(element).perform() #单击某元素
    ActionChains(driver).click_and_hold(element).perform() #在此元素上按下左键不放
    ActionChains(driver).context_click(element).perform() #在此元素上单击右键
    ActionChains(driver).double_click(element).perform() #在此元素上双击

    鼠标拖拽:

    ActionChains(driver).drag_and_drop(source,target).perform() #从一个元素的位置,拖至另一个元素位置松开
    ActionChains(driver).drag_and_drop_by_offset(source,xoffset,yoffset).perform()#以坐标的形式拖拽,x,y

    鼠标移动:

    ActionChains(driver).move_by_offset(x,y).perform()#移动到(x,y)坐标位置 
    ActionChains(driver).move_to_element(element).perform()#鼠标移动到某个元素上
    ActionChains(driver).move_to_element_with_offset(element,x,y).perform()#移动到某个元素上,然后,在移动到相对坐标(x,y)上
  • 相关阅读:
    本地代码库关联Github
    常用正则表达式
    IDEA开启并配置services窗口
    数据结构总结
    IDEA导入项目后文件出现时钟的原因及解决方案
    win7硬盘安装过程图解(需要编辑)
    How to Create a Automated Task that Runs at a Set Time in Windows 7
    【转】Code Review(代码复查)
    (收藏)C#开源资源大汇总
    Windows Workflow Foundation:向跟踪服务(TrackingService)传递数据
  • 原文地址:https://www.cnblogs.com/ronyjay/p/12889485.html
Copyright © 2011-2022 走看看