需要导入的包:
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元素上