winappdriver对已经打开的桌面软件做自动化

原因:软件无法从安装目录双击exe文件打开

解决方案:之前不知道winappdriver可以控制已经打开的窗口,后来在网络上找到办法,winappdriver可以把桌面当成一个窗口,先枚举桌面上的窗口,然后根据窗口名称获取到我们要测试的窗口的handle,此时就可以用这个句柄来新建一个driver。

参考:https://github.com/microsoft/WinAppDriver/blob/v1.0/README.md

https://blog.csdn.net/weixin_42152949/article/details/122255214

复制代码
from appium import webdriver
from selenium.webdriver.common.by import By

desired_caps = {'app': 'Root', 'deviceName': 'windowsPC', 'platformName': 'Windows'}
driver = webdriver.Remote(
    command_executor='http://127.0.0.1:4723',
    desired_capabilities=desired_caps)

main_win = driver.find_element(By.NAME, 'xxxxxxxx')
print(main_win)
hd = hex(int(main_win.get_attribute("NativeWindowHandle")))
print(hd)
caps = {'appTopLevelWindow': str(hd)}
driver = webdriver.Remote(
    command_executor='http://127.0.0.1:4723',
    desired_capabilities=caps)
复制代码

注:从这段代码可以看出,针对没有打开的软件,和已经打开的软件,新建driver时唯一的区别就是参数caps的区别,前者是app,后者是appTopLevelWindow

posted @   LvT0.0  阅读(914)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示