zoukankan      html  css  js  c++  java
  • python+selenium_鼠标事件

    引言--在实际的web产品测试中,对于鼠标的操作,不单单只有click(),有时候还要用到右击、双击、拖动等操作,这些操作包含在ActionChains类中。

    一、ActionChains类中鼠标操作常用方法:

     context_click() :右击
     double_click() :双击
    drag_and_drop() :拖动      
    move_to_element() :鼠标移动到一个元素上

     举例:

    #cording=gbk
    import os
    from selenium import webdriver
    from selenium.webdriver.common.by import By #导入by方法
    from selenium.webdriver.common.action_chains import ActionChains ##对鼠标事件操作

    current_path=os.path.dirname(__file__)
    firefox_path=current_path+"/../webdriver/geckodriver.exe"
    driver=webdriver.Firefox(executable_path=firefox_path)
    driver.get("http://127.0.0.1/zentao/user-login-L3plbnRhby9teS5odG1s.html")

    mouse=ActionChains(driver)  #创建一个鼠标对象
    # element1=driver.find_element(By.XPATH,"//img[@src='/zentao/theme/default/images/main/zt-logo.png']") #Xpath利用属性定位
    element1=driver.find_element(By.XPATH,"//img[contains(@src,'images/main/zt-logo.png')]") #xpath使用包含属性方法定位
    mouse.context_click(element1).perform() #执行鼠标右击,.perform() 表示执行

    element2=driver.find_element(By.XPATH,"//button[@type='button' and @class='btn' ]")  #多属性定位
    mouse.move_to_element(element2).perform() #移动到这个元素上

    #对元素进行截图
    driver.find_element(By.XPATH,"//button[@id='submit'][@type='submit']").screenshot('element1.png')
  • 相关阅读:
    String类的substring方法
    postman绕过登录,进行接口测试的方法
    Dubbo
    那些吊炸天的互联网名词
    版本控制工具git
    Ubunto20.04 sudo apt-get update 出现目标被重置多次!
    ubuntu环境下搭建Hadoop集群中必须需要注意的问题
    Python作业---内置数据类型
    python作业完成简单的文件操作
    python3实现计算器
  • 原文地址:https://www.cnblogs.com/123anqier-blog/p/12729403.html
Copyright © 2011-2022 走看看