zoukankan      html  css  js  c++  java
  • selenium-模拟鼠标

    需要导入的包:

    from selenium.webdriver import ActionChains

    一、模拟鼠标右键

    ActionChains(self.driver).context_click(xxx).perform()  

    # coding=UTF-8
    #19.模拟鼠标右键
    import sys
    reload(sys)
    sys.setdefaultencoding('utf8')
    from selenium import webdriver
    import unittest
    import time
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver import ActionChains
    
    class Case18(unittest.TestCase):
    
        def setUp(self):
            self.driver = webdriver.Chrome()
    
        def test_simulateASingleKey(self):
            url = "https://www.sogou.com"
            self.driver.get(url)
            element = self.driver.find_element_by_id("query")
            element.send_keys("selenium")
            time.sleep(2)
            element.send_keys(Keys.CONTROL, 'a')  # c trl+a 全选输入框内容
            time.sleep(2)
            element.send_keys(Keys.CONTROL,'x') # ctrl+x 剪切输入框内容
            time.sleep(2)
            ActionChains(self.driver).context_click(element).perform() # 鼠标右击单击
            time.sleep(2)
            ActionChains(self.driver).send_keys('P').perform() #发送黏贴命令,字符P代表黏贴 (只支持IE浏览器)
            time.sleep(2)
    
        def tearDown(self):
            self.driver.quit()
    
    if __name__ == '__main__':
        unittest.main()

    二、模拟鼠标左键按下与释放

    ActionChains(self.driver).click_and_hold(xxx).perform() ——在xxx元素上执行按下鼠标左键并保持
    ActionChains(self.driver).release(xxx).perform() ——在xxx元素上释放一直按下的鼠标左键

    三、保持鼠标悬停在某个元素上

    ActionChains(self.driver).move_to_element(xxx).perform() ——将鼠标悬浮到xxx元素上

  • 相关阅读:
    svn cleanup failed–previous operation has not finished 解决方法
    开源SNS社区系统推荐
    从网络获取图片本地保存
    MS SQL Server 数据库连接字符串
    KeepAlive
    Configure Git in debian
    sqlserver query time
    RPi Text to Speech (Speech Synthesis)
    SQL Joins with C# LINQ
    search or reseed identity columns in sqlserver 2008
  • 原文地址:https://www.cnblogs.com/erchun/p/11800806.html
Copyright © 2011-2022 走看看