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)上
  • 相关阅读:
    SpringCloud笔记(一)服务注册与发现
    个人备忘录
    ActiveMQ 消息持久化到Mysql数据库
    染色 [组合数 容斥]
    各种图床
    NOIP2012 疫情控制
    网格计数
    找钱 [多重背包 计数]
    序列[势能线段树]
    牛客挑战赛33 B-鸽天的放鸽序列
  • 原文地址:https://www.cnblogs.com/ronyjay/p/12889485.html
Copyright © 2011-2022 走看看