🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
1、Python访问剪贴板 ~~~bash $ sudo pip2 install pyperclip ~~~ ### 测试使用 ~~~python >>>import pyperclip >>>pyperclip.copy("this is a test") >>>pyperclip.paste() this is a test ~~~ 可以先用鼠标选中一部分文字进行复制,然后运行`pyperclip.paste()`,你会发现输出的就是你复制的内容.如果先用`pyperclip.copy("this is a test")`,再用鼠标进行粘贴也会是this is test. 总结来说: * pyperclip.copy()用于将文字复制进剪切板 * pyperclip.paste()用于将文字从剪切板复制出来 # ## **PyAutoGUI程序自动化控制** PyAutoGUI是一个款Python的GUI自动化工具,其目的是利用脚本控制鼠标和键盘操作。PyAutoGUI 适用于 Windows、macOS 和 Linux,并在 Python 2 和 3 上运行。 在已安装Python环境的基础上通过pip安装 PyAutoGUI库: pip install -i https://pypi.douban.com/simple pyautogui 源码地址:https://github.com/asweigart/pyautogui 功能: 移动鼠标,单击或键入其他应用程序的窗口。 向应用程序发送击键(例如,填写表单)。 截屏,并给出一个图像(例如,按钮或复选框),在屏幕上找到它。 找到应用程序的窗口,并移动、调整大小、最大化、最小化或关闭它(当前仅使用 Windows) 在 GUI 自动化脚本运行时显示用户交互的消息框。 # 【python+WinAppDriver】windows自动化测试 最近有些忙,随便写写吧 先安装winappdriver,使用的话就像appium一样,开启服务一直运行着 代码也跟移动端一样,直接看吧 import unittest from appium import webdriver from selenium.webdriver.common.keys import Keys import time desired_caps = {} desired_caps['app'] = r"notepad.exe" driver = webdriver.Remote( command_executor='http://127.0.0.1:4723', desired_capabilities=desired_caps) driver.find_element_by_name("文本编辑器").send_keys("polyv") time.sleep(5) driver.quit() ***** # [聊聊 PC 端自动化最佳方案 - WinAppDriver](https://blog.csdn.net/qq_39241986/article/details/119834169) ~~~php 人人都可以简单入门Python、爬虫、数据分析 简说Python推荐 来源:AirPython作者:星安果 ~~~ ## 1\. 前言 一提到自动化,可能大家想到的是 App 端的 Appium、Airtest、AutoJS,亦或是 Selenium、Puppeteer、Cypress 等 Web 端的自动化框架 本篇文章,我将和大家聊聊 PC 端的自动化工具 - WinAppDriver ## 2\. 准备 WinAppDriver,全称为 Windows Application Driver,它是 Windows 上一个类似 Selenium 的 UI 自动化驱动服务框架 它支持 Appium,可以使用 Appium-[Python](https://so.csdn.net/so/search?from=pc_blog_highlight&q=Python)\-Client 依赖库完成对 Windows 桌面程序的自动化操作 项目地址:https://github.com/Microsoft/WinAppDriver 需要注意的是,要使用 WinAppDriver 服务框架完成 Windows 的自动化,需要满足 Windows10 或 Windows Server 2016 以上系统 另外,它支持的应用程序包含: * UWP  -  Universal Windows Platform * WinForms  -  Windows Forms * WPF  -  Windows Presentation Foundation * Win32  -  Classic Windows 在实现之前,我们需要做好以下准备工作 2-1  开启「 开发者模式 」 关键字搜索「 开发者设置 」,选择开启「 开发者模式 」 ![](https://img-blog.csdnimg.cn/img_convert/5e438244a756b6db3c2950c04ae45676.png) 2-2  安装窗口组件元素识别工具 常用的 2 种窗口元素识别工具为:inspect.exe、FlaUInspect 其中 作为官方的组件元素识别工具,inspect.exe 集成于 Windows SDK 如果本地不存在该文件,可以通过下面链接进行安装 https://download.microsoft.com/download/4/d/2/4d2b7011-606a-467e-99b4-99550bf24ffc/windowssdk/winsdksetup.exe 相比 inspect.exe,FlaUInspect 界面更简洁,功能更易用( 推荐 ) 项目地址:https://github.com/FlaUI/FlaUInspect 2-3  安装 WinAppDriver 通过下面链接下载 WinAppDriver 应用程序,并在本地运行起来 https://github.com/Microsoft/WinAppDriver/releases 2-4  搭建 Appium 环境 这部分内容涉及 NodeJS 安装及 Appium-Server 环境的搭建 可以参考:https://www.cnblogs.com/amoyshmily/p/10500687.html 2-5  安装依赖 最后安装 Python 依赖库 Appium-Python-Client ~~~go # 安装依赖 Appium-Python-Clientpip3 install Appium-Python-Client ~~~ ## 3. 实战一下 我们以操作 PC 端的微信为例,聊聊自动化的常见步骤 首先,我们在本机打开 WinAppDriver 服务,让它在后台运行 然后,我们使用 Python 编写自动化脚本 通过 ip 地址、端口号及 PC 版微信的绝对路径,使用 Appium 打开微信 ``` import time, osfrom appium  import webdriverfrom selenium.webdriver  import ActionChainsfrom selenium.webdriver.common.keys  import Keysfrom time import sleep class Auto():     def open_weixin(self, host='localhost', port=4723):        # 打开WinAppDriver服务         # 注意:如果手动开启,则可以注释掉        # os.system(r'start "" /d "C:\Program Files\Windows Application Driver\"  "WinAppDriver.exe"')         # 配置信息        # 包含:平台名、系统、应用程序绝对路径         desired_caps = {'platformName': 'Windows', 'deviceName': 'WindowsPC',  'app': r"D:\Program Files (x86)\Tencent\WeChat\WeChat.exe"}          try:             # 连接WinAppDriver服务,打开目标软件             self.driver = webdriver.Remote('http://{}:{}'.format(host, port), desired_caps)         except Exception as e:             raise AssertionError(e) ``` 接着,通过「 组件元素识别工具 」拿到界面元素的属性值,执行常见的点击、移动、滑动等操作 比如:点击「 文件传输助手 」,发送一条信息 ~~~go # 给文件传输助手发送一条信息def send_msg(self, element_name, msg):    """    :param element_name:元素name值    :param msg:    :return:    """    # 通过name属性,找到目标元素    chat_element = self.weixin_driver.find_element_by_name(target_name)     # 点击元素,进入聊天界面    chat_element.click()     # 找到输入框,并输入    self.weixin_driver.find_element_by_name("输入").send_keys(msg)     # 点击右下角的发送,发送消息出去    self.weixin_driver.find_element_by_name("发送(S)").click() ~~~ 需要注意的是,如果涉及界面的滑动,可以使用「 ActionChains 」移动鼠标,然后使用 win32api 和 win32con 模拟屏幕滑动即可 ~~~go import win32apiimport win32confrom appium import webdriverfrom selenium.webdriver import ActionChains # 模拟屏幕滑动# 1、移动到某个元素区域ActionChains(self.weixin_driver).move_to_element(     self.weixin_driver.find_element_by_name("element_name")).perform() # 2、滑动界面# 比如,向上滚动,模拟滑动win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, -500) ~~~ 完成自动化操作后,就可以主动释放资源、关闭 WinAppDriver 服务 ~~~go # 释放资源及关闭服务def tearDownFunc(self):    print("准备退出")    sleep(2)     # 1、释放资源    self.weixin_driver.quit()     # 2、关闭WinAppDriver应用程序    os.system(' @taskkill /f /im WinAppDriver.exe') ~~~ ## 4\. 最后 在实际使用过程中,可能会遇到复杂的桌面应用程序,这时我们可以通过打印驱动对象的「 page\_source」元素控制树值,以此来帮助我们进行快速定位元素,进而完善自动化脚本 如果你觉得文章还不错,请大家 **点赞、分享、留言** 下,因为这将是我持续输出更多优质文章的最强动力!