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

    鼠标事件

    在webdriver中,将这些关于鼠标操作的方法封装在ActionChains类提供。

    1. ActionChains类中常用的鼠标事件:
    2. perform():执行所有ActionChains中存储的行为
    3. context_click():右击
    4. double_click():双击
    5. drag_and_drop():拖动
    6. move_to_element():鼠标悬停
    from selenium import webdriver
    #导入提供鼠标事件的ActionChains
    from selenium.webdriveer.common.action_chains import ActionChains
    
    driver = webdriver.Chrome()
    driver.get('https://www.baidu.com')
    #定位到悬停的元素
    above = driver.find_element_by_link_text('设置')
    #对定位到的元素进行鼠标悬停操作
    ActionChains(driver).move_to_element(above).perfrom()

    from selenium.webdriver import ActionChains

    >>导入提供鼠标操作的ActionChains类。

    ActionChains(driver)

    >>调用ActionChains()类,将浏览器驱动driver作为参数传入

    move_to_element(above)

    >>context_click()方法用于模拟鼠标右键操作,在调用时需要指定元素定位

    perform()

    >>执行所有的ActionChains中存储的行为,可以理解成对整个操作的提交动作。

    实例:

    coding = utf-8
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import time
    from selenium.webdriver.common.action_chains import ActionChains
    
    driver = webdriver.Firefox()
    driver.get("http://172.16.10.7/bugfree/index.php/site/login")
    #登陆
    driver.find_element_by_id("LoginForm_username").send_keys("solo")
    driver.find_element_by_id("LoginForm_username").send_keys(Keys.TAB)  #昨天的知识点Keys.TAB
    driver.find_element_by_id("LoginForm_password").send_keys("test")
    driver.find_element_by_id("LoginForm_password").send_keys(Keys.ENTER)
    driver.find_element_by_id("LoginForm_rememberMe").click()
    time.sleep(5)
    #定位要点击的元素,先定义一个变量
    problemlist = driver.find_element_by_xpath("html/body/div/div[2]/div[5]/div[3]/div[3]/table/tbody/tr[1]/td[5]/span/a")
    #driver.find_element_by_xpath("//a[contains(@href,'/bugfree/index.php/case/list/1')]")
    
    ActionChains(driver).context_click(problemlist).perform() #对定位到的元素执行鼠标右键操作
    ActionChains(driver).double_click(problemlist).perform() #对定位到的元素执行鼠标双击操作
    
    #报错:Missing or invalid type argument for pointer action,还没有定位出原因
    
    driver.quit()
  • 相关阅读:
    CSS3新增文本属性
    CSS选择器
    【转】Java基础——面试题汇总
    【转】equals和==的区别
    【转】JRE和JDK的区别
    【转】深度学习常用的模型评估指标
    【转】使用Scanner输入字符串时next()和nextLine()区别
    JAVA操作ORACLE大对象
    ORACLE大对象存储
    iis是什么东西?
  • 原文地址:https://www.cnblogs.com/aszeno/p/10309346.html
Copyright © 2011-2022 走看看