WebDriver API 之模拟组合按键操作

支持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.driver = webdriver.Chrome()
		self.driver = webdriver.Firefox()
		self.driver.maximize_window()
	def test_simulationCombinationKeys(self):
		url = "http://www.baidu.com/"
		self.driver.get(url)
		input = self.driver.find_element_by_id('kw')
		input.clear()
		input.click()
		input.send_keys(u'光荣之路')
		sleep(3)
		ActionChains(self.driver).key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()
		sleep(3)
		ActionChains(self.driver).key_down(Keys.CONTROL).send_keys('x').key_up(Keys.CONTROL).perform()
		sleep(3)
		url = "http://www.sogou.com/"
		self.driver.get(url)
		ActionChains(self.driver).key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
		sleep(3)
		self.driver.find_element_by_id('stb').click()
		sleep(3)
		self.assertTrue(u"光荣之路 - 搜狗搜索" in self.driver.page_source,u"没有打开网页")
	def tearDown(self):
		self.driver.quit()
if __name__ == '__main__':
	unittest.main()

二、通过设置剪贴板实现复制和粘贴

需要导入的依赖模块:

import win32clipboard as w
import win32con
测试代码:
from selenium import webdriver
import unittest
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import pywin
import win32clipboard as w
import win32con
from time import sleep
def setText(aString):
	w.OpenClipboard()
	w.EmptyClipboard()
	# w.SetClipboardData(win32con.CF_UNICODETEXT,aString)
	w.SetClipboardText(aString)
	w.CloseClipboard()
def getText():
	w.OpenClipboard()
	d=w.GetClipboardData(win32con.CF_TEXT)
	w.CloseClipboard()
	return d
class combinationKeys(unittest.TestCase):
	def setUp(self):
		# self.driver = webdriver.Chrome()
		self.driver = webdriver.Firefox()
		self.driver.maximize_window()
	def test_copyAndPaste(self):
		url="http://www.baidu.com/"
		self.driver.get(url)
		content=u"光荣之路"
		# content=u"python"
		setText(content)
		getContent=getText()
		print(getContent.decode("gbk").encode("utf-8"))
		self.driver.find_element_by_id("kw").click()
		ActionChains(self.driver).key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
		sleep(3)
		self.driver.find_element_by_id('su').click()
		sleep(3)
	def tearDown(self):
		self.driver.quit()
if __name__ == '__main__':
	unittest.main()

结果有乱码:(未解决)

b'\xb9\xe2\xc8\xd9\xd6\xae\xc2\xb7'
.
----------------------------------------------------------------------
Ran 1 test in 19.668s

OK
[Finished in 20.3s]

你可能感兴趣的:(自动化测试)