PC应用自动化

一、运行条件

1、下载安装winappdriver,在Windows设置中启用开发者模式,运行WinAppDriver.exe(默认:C:\Program Files (x86)\Windows Application Driver)

2、下载 Windows SDK,主要使用自带的inspect.exe工具进行UI元素检查(如:C:\Program Files (x86)\Windows Kits\10\bin\10.0.22000.0\x64)

3、安装第三方包:Appium-Python-Client

参考连接:

官方地址

定位方式-启动参数

二、初次运行

import time
import unittest
from appium import webdriver


class SimpleCalculatorTests(unittest.TestCase):
    driver = None
    @classmethod
    def setUpClass(self):
        desired_caps = {}
        desired_caps["app"] = "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App"
        desired_caps["platformName"] = "Windows"
        desired_caps["platformVersion"] = "10"
        self.driver = webdriver.Remote(
            command_executor='http://127.0.0.1:4723',
            desired_capabilities=desired_caps)

    @classmethod
    def tearDownClass(self):
        self.driver.quit()

    def test_output(self):
        time.sleep(5)
        print("运行成功")

if __name__ == '__main__':
    unittest.main()

三、定位参考元素

Windows 应用程序驱动程序支持各种定位器以在应用程序会话中查找 UI 元素。下表显示了所有支持的定位器策略及其相应的 UI 元素属性显示在inspect.exe 中

Client API Locator Strategy Matched Attribute in inspect.exe Example
FindElementByAccessibilityId accessibility id AutomationId AppNameTitle
FindElementByClassName class name ClassName TextBlock
FindElementById id RuntimeId (decimal) 42.333896.3.1
FindElementByName name Name Calculator
FindElementByTagName tag name LocalizedControlType (upper camel case) Text
FindElementByXPath xpath Any //Button[0]
inspect.exe 上的RuntimeId为16进制的,如果自己转换为十进制(如: [2A.160A90.4.3C] 转换为=> '42.1444496.4.60')
 注意:RuntimeId每次都会改变,所以建议使用:AutomationId 、Name或者xpath的方式来定位
self.driver.find_element_by_accessibility_id("num1Button").click()
self.driver.find_element_by_name("一").click()
self.driver.find_element_by_xpath("//*[@Name='一']").click()

四、启动参数

Capabilities Descriptions Example
app Application identifier or executable full path Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge
appArguments Application launch arguments https://github.com/Microsoft/WinAppDriver
appTopLevelWindow Existing application top level window to attach to 0xB822E2
appWorkingDir Application working directory (Classic apps only) C:\Temp
platformName Target platform name Windows
platformVersion Target platform version 1.0
        desired_caps["app"] = r"C:\WINDOWS\system32\notepad.exe"
        desired_caps["platformName"] = "Windows"
        desired_caps["platformVersion"] = "10"
        desired_caps["appArguments"] = "TT.txt"
        desired_caps["appWorkingDir"] = r"D:\Error"

五、例子

import time
import unittest
from appium import webdriver



class SimpleCalculatorTests(unittest.TestCase):
    driver = None
    @classmethod
    def setUpClass(self):
        desired_caps = {}
        desired_caps["app"] = r"D:\Program Files (x86)\Tencent\WeChat\WeChat.exe"
        desired_caps["platformName"] = "Windows"
        desired_caps["platformVersion"] = "10"
        self.driver = webdriver.Remote(
            command_executor='http://127.0.0.1:4723',
            desired_capabilities=desired_caps)
        self.driver.implicitly_wait(30)
    @classmethod
    def tearDownClass(self):
        self.driver.quit()

    def test_output(self):
        print("运行成功")
        self.driver.find_element_by_name("文件传输助手").click()
        self.driver.find_element_by_name("输入").send_keys("123456")
        self.driver.find_element_by_name("发送(S)").click()
        time.sleep(5)


if __name__ == '__main__':
    unittest.main()

import time
import unittest
from appium import webdriver
from pynput.keyboard import Key, Controller


class SimpleCalculatorTests(unittest.TestCase):
    driver = None

    @classmethod
    def setUpClass(self):
        desired_caps = {}
        desired_caps["app"] = r"D:\Program Files (x86)\Tencent\QQ\Bin\QQ.exe"
        desired_caps["platformName"] = "Windows"
        desired_caps["platformVersion"] = "10"
        self.driver = webdriver.Remote(
            command_executor='http://127.0.0.1:4723',
            desired_capabilities=desired_caps)
        self.driver.implicitly_wait(30)
        self.keyboard = Controller()

    @classmethod
    def tearDownClass(self):
        self.driver.quit()

    def test_output(self):
        print("运行成功")
        element = self.driver.find_element_by_xpath("//*[@Name='QQ号码']/*[@Name='']")
        element.click()
        time.sleep(5)
        self.keyboard.press(Key.delete)
        time.sleep(5)
        self.keyboard.type("99999999")
        time.sleep(5)
        self.driver.find_element_by_name("登录").click()
        time.sleep(5)


if __name__ == '__main__':
    unittest.main()

Logo

一站式 AI 云服务平台

更多推荐