zoukankan      html  css  js  c++  java
  • selenium之鼠标的操作(python)

    鼠标操作的方法,封装在ActionChains类中

    perform:执行ActionChains中的所有存储行为

    context_click:右键单击

    move_to_element:悬停

    double_click:双击

    drag_and_drop:拖动

    1.右击操作

     1 #!/usr/bin/env python
     2 # _*_ coding:utf-8 _*_
     3 from selenium import webdriver
     4 from selenium.webdriver import ActionChains
     5 from time import sleep
     6 
     7 driver = webdriver.Firefox()
     8 driver.get("https://www.baidu.com/")
     9 # 右击操作
    10 sleep(5)
    11 right_click = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[3]/a[1]")    #新闻
    12 ActionChains(driver).context_click(right_click).perform()

    2.悬停

     1 #!/usr/bin/env python
     2 # _*_ coding:utf-8 _*_
     3 from selenium import webdriver
     4 from selenium.webdriver import ActionChains
     5 from time import sleep
     6 
     7 driver = webdriver.Firefox()
     8 driver.get("https://www.baidu.com/")
     9 # 悬停操作
    10 sleep(5)
    11 on_stop = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[3]/a[8]")  #设置
    12 ActionChains(driver).move_to_element(on_stop).perform()

    3.双击

     1 #!/usr/bin/env python
     2 # _*_ coding:utf-8 _*_
     3 from selenium import webdriver
     4 from selenium.webdriver import ActionChains
     5 from time import sleep
     6 
     7 driver = webdriver.Firefox()
     8 driver.get("https://www.baidu.com/")
     9 # 双击操作
    10 sleep(5)
    11 double_click = driver.find_element_by_xpath('//*[@id="su"]')  #百度一下
    12 ActionChains(driver).double_click(double_click).perform()

    4.拖动,方法类似,在此不列举

  • 相关阅读:
    创建nodejs服务器
    研磨设计模式学习笔记2外观模式Facade
    研磨设计模式学习笔记4单例模式Signleton
    研磨设计模式学习笔记1简单工厂(SimpleFactory)
    getResourceAsStream小结
    研磨设计模式学习笔记3适配器模式Adapter
    oracle数据库代码块
    DecimalFormat
    .NET中常用的代码(转载)
    WebClient的研究笔记
  • 原文地址:https://www.cnblogs.com/zhangyating/p/8386489.html
Copyright © 2011-2022 走看看