支持Python模拟windows组合按键的包:pywin
一、通过webDriver内建的模块模拟组合键
首先需要导入依赖包:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
模拟组合键ctrl+c:
ActionChains(self.driver).key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()
其中,key_down(Keys.CONTROL)表示按下Ctrl键;send_keys('c')表示输入c键;key_up(Keys.CONTROL)表示释放Ctrl键。
注意:selenium3.x的ActionChains模块对某些浏览器的某些版本的支持不是很好,建议使用Firefox浏览器。
测试代码:
from selenium import webdriver
import unittest
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from time import sleep
class combinationKeys(unittest.TestCase):
def setUp(self):
# self.dri