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)上
  • 相关阅读:
    Python元组、列表、字典
    测试通过Word直接发布博文
    Python环境搭建(windows)
    hdu 4003 Find Metal Mineral 树形DP
    poj 1986 Distance Queries LCA
    poj 1470 Closest Common Ancestors LCA
    poj 1330 Nearest Common Ancestors LCA
    hdu 3046 Pleasant sheep and big big wolf 最小割
    poj 3281 Dining 最大流
    zoj 2760 How Many Shortest Path 最大流
  • 原文地址:https://www.cnblogs.com/ronyjay/p/12889485.html
Copyright © 2011-2022 走看看