微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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元素上

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐