• python_selenium_鼠标操作


    一。鼠标操作

    调用:ActionChains模块

    公用模块代码:

    from selenium import webdriver
    from selenium.webdriver import ActionChains
    #ActionChains - - ->鼠标操作模块
    driver=webdriver.Chrome()
    driver.get("http:www.baidu.com")
    driver.maximize_window()
    #定位“设置”按钮的位置
    ele_shehzi=driver.find_element_by_id("s-usersetting-top")
    action_chains=ActionChains(driver)

    1.点击

    #进行点击
    #方式一
    #ele_shehzi.click()
    #方式二
    action_chains.click(ele_shehzi)

    2.双击

    3.拖拽

    4.悬停

    1.悬停

    #悬停并点击
    action_chains.move_to_element(ele_shehzi).click().perform()

    2.悬停获取浮动类型数据坐标并进行操作

    #悬停后选择该类型的位置如:“高级搜索”
    h_element=driver.find_element_by_link_text("高级搜索")
    h_element.click()


    注意:
    链式调用:
    1.最后需要使用perform()方法启用(ActionChains模块使用的时链式调用:初始化列表把所有方法放到列表中,在选择方法【可以选择多个方法】后最后使用perform()激活调用)
    以move_to_element方法为例说明
    1)init中建立空列表:

        2)把move_to_element方法新增到self._actions列表中,并返回self

     

      3)使用perform()方法进行激活完成调用(最终把匹配到的方法都以实例返回完成调用)

      

     例子:

    _actions = []
    def move_to():
    print("移动。。。")

    def click():
    print("点击")

    _actions.append(click)
    _actions.append(move_to)

    def perform():
    for action in _actions:
    action()

    perform()
    运行结果:

    5.双击

    #双击
    #d定位“百度一下”按钮坐标
    butter_baidu=driver.find_element_by_id("su")
    #双击元素
    #action_chains.double_click(butter_baidu)
    #对元素进行右键单击
    action_chains.context_click(butter_baidu)
    #在源元素上按住鼠标左键,然后移动到目标元素并释放鼠标按钮
    #source:来源 ;target:目标
    action_chains.drag_and_drop(source,target)


    6.确认回车操作与数据提交方法
    #使用系统回车键
    #系统按钮操作地址from selenium.webdriver.common.keys import Keys
    #ele_shurubaidu.send_keys(Keys.ENTER)

    #使用submit提交数据
    #注意:提交数据时需要查看是否在form表单中
    time.sleep(2)
    ele_shurubaidu.submit()
    form表单查看地址:

    7.选择框操作

    from selenium import webdriver
    from selenium.webdriver import ActionChains
    #ActionChains - - ->鼠标操作模块
    driver=webdriver.Chrome()
    driver.get("http:www.baidu.com")
    driver.maximize_window()
    #定位“设置”按钮的位置
    ele_shehzi=driver.find_element_by_id("s-usersetting-top")
    #进行点击
    #方式一
    #ele_shehzi.click()
    #方式二
    ActionChains(driver).click(ele_shehzi)
  • 相关阅读:
    Dubbo源码分析之ExtensionLoader加载过程解析
    大型网站系统与java中间件实践-阅读笔记
    多线程编程-设计模式之保护性暂挂(Guarded Suspesion)模式
    多线程编程-设计模式之不可变对象模式
    Spring技术内幕阅读笔记(一)
    mysql存储过程语法及实例
    Spring-boot官方案例分析之data-jpa
    Spring-boot官方案例分析之log4j
    Spring Boot应用的测试——Mockito
    linux系统安装redis
  • 原文地址:https://www.cnblogs.com/newsss/p/13340633.html
Copyright © 2020-2023  润新知