C端自动化实现:appium+winappdriver+python
一. 前言
有小伙伴有办公自动化的需求,特此出一篇C端自动化教程,并附带demo案例。C端的自动化比B端多一个appium,其他的操作大同小异。
二. 环境
- appium:exe工具,用于启动服务,官网下载。
- Appium-Python-Client :python驱动appnium的库,使用pip安装。
- selenium:ui自动化库,appium的核心层,使用pip安装。
- inspect:exe工具,用于定位元素,官网下载。
- winappdriver:是pc端应用做ui自动化的桥梁,官网下载。
- 开发者模式:在设置-开发者模式里,选择开启。
三. 案例
以操作谷歌浏览器的搜索框为例,采用C端的方式做UI自动化,代码如下:
import subprocess import time from appium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as ec def search(): """ 需求:使用google浏览器搜索 “测神-博客园” """ # 启动winappdriver subprocess.Popen(r'start "" /d ""D:\s\Windows Application Driver\" "WinAppDriver.exe"', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # 配置c端应用程序 desired_caps = {"app": r"C:\Program Files\Google\Chrome\Application\chrome.exe"} # 启动应用程序 driver = webdriver.Remote( command_executor="http://127.0.0.1:4723", desired_capabilities=desired_caps ) # 获取应用程序界面的元素(元素定位通过inspect工具实现) search_box = WebDriverWait(driver, 20).until(ec.presence_of_element_located((By.NAME, "地址和搜索栏"))) # 操作元素 search_box.click() search_box.send_keys('博客园-测神') search_box.send_keys(Keys.ENTER) # 退出浏览器 time.sleep(5) driver.quit() if __name__ == '__main__': search()
四. 后记
截止目前(2022/6/21),发现Appium并没有远程启动入口,这意味着未来其官方会缺少对pc端自动化的支持。
不过这未尝不是一件好事,复杂的配置已经劝退不少人了,连app自动化用这个做都觉得很烂。
如果不想用旧版本的,那么用什么做pc端自动化呢?
——pywinauto库。
后续我会出一篇教程的,这个做pc端自动化要方便多了,而且少了这些复杂的配置,省事儿不少。