windows UIAutomation

本文详细介绍了一种基于UIAtuomationClient和log4net的日志记录与UI自动化测试框架的实现方法。从添加必要的dll文件开始,到定义辅助类进行元素定位与操作,再到具体的应用示例,为读者提供了全面的指导。

1、第一步:添加引用UIAtuomationClient.dll,UIAtutomationTypes.dll

2、第二步:添加log4net.dll日志类。

3、辅助类

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Drawing;
using System.Threading;
using System.Windows.Automation;
using System.Runtime.InteropServices;

namespace YunDouTaxImport.Utils
{
    public class UIAutoHelper
    {
        /// <summary>
        /// 日志辅助
        /// </summary>
        private static readonly ILog Log = LogManager.GetLogger(nameof(UIAutoHelper));

        /// <summary>
        /// 获取桌面的根
        /// </summary>
        /// <value>
        /// The elmnt root.
        /// </value>
        public static AutomationElement ElmntRoot
        {
            get { return AutomationElement.RootElement; }
        }

        #region 创建条件对象

        /// <summary>
        /// 通过名称获取条件
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static PropertyCondition GetConditionWithName(string name)
        {
            return GetCondition(AutomationElement.NameProperty, name);
        }

        /// <summary>
        /// 通过类名称获取条件
        /// </summary>
        /// <param name="className">Name of the class.</param>
        /// <returns></returns>
        public static PropertyCondition GetConditionWithClassName(string className)
        {
            return GetCondition(AutomationElement.ClassNameProperty, className);
        }

        /// <summary>
        /// 通过AutomationId获取条件
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public static PropertyCondition GetConditionWithAutomationId(string id)
        {
            return GetCondition(AutomationElement.AutomationIdProperty, id);
        }

        /// <summary>
        /// 通过ProcessId获取条件
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public static PropertyCondition GetConditionWithProcessId(int id)
        {
            return GetCondition(AutomationElement.ProcessIdProperty, id);
        }

        /// <summary>
        /// 通过条件组合获取
        /// </summary>
        /// <param name="array">The array.</param>
        /// <returns></returns>
        public static Condition GetConditionFromArray(PropertyCondition[] array)
        {
            return new AndCondition(array);
        }


        private static PropertyCondition GetCondition(AutomationProperty property, object val)
        {
            return new PropertyCondition(property, val);
        }
        #endregion 创建条件对象

        #region 查找界面元素

        /// <summary>
        /// 通过父节点查找第一个字节点元素(只针对下一级元素)
        /// </summary>
        /// <param name="countRetry">The count retry.</param>
        /// <param name="interval">The interval.</param>
        /// <param name="parent">The parent.</param>
        /// <param name="condition">The condition.</param>
        /// <returns></returns>
        public static AutomationElement FindFirstOfChildren(int countRetry,int interval,AutomationElement parent,Condition condition)
        {
            for(int i = 0; i < countRetry; i++)
            {
                try
                {
                    var elmnt = parent.FindFirst(TreeScope.Children, condition);
                    if (elmnt != null)
                    {
                        return elmnt;
                    }
                }
                catch(Exception e)
                {
                    Log.Error(string.Format("用Children参数调用FindFirst方法时出现异常,异常消息:{0}", e.Message));
                }
                Thread.Sleep(interval);
            }
            return null;
        }

        /// <summary>
        /// 通过父节点查找所有的子节点(只针对下一级)
        /// </summary>
        /// <param name="countRetry">The count retry.</param>
        /// <param name="interval">The interval.</param>
        /// <param name="parent">The parent.</param>
        /// <param name="condition">The condition.</param>
        /// <returns></returns>
        public static AutomationElementCollection FindAllOfChildren(int countRetry, int interval, AutomationElement parent, Condition condition)
        {
            for (int i = 0; i < countRetry; i++)
            {
                try
                {
                    var elmnt = parent.FindAll(TreeScope.Children, condition);
                    if (elmnt != null)
                    {
                        return elmnt;
                    }
                }
                catch (Exception e)
                {
                    Log.Error(string.Format("用Children参数调用FindAll方法时出现异常,异常消息:{0}", e.Message));
                }
                Thread.Sleep(interval);
            }
            return null;
        }

        /// <summary>
        /// 通过父节点查找所有的子节点中的第一个满足条件的子节点
        /// </summary>
        /// <param name="countRetry">重试次数</param>
        /// <param name="interval">时间间隔.</param>
        /// <param name="parent">父节点</param>
        /// <param name="condition">条件.</param>
        /// <returns></returns>
        public static AutomationElement FindFirstOfSubtree(int countRetry, int interval, AutomationElement parent, Condition condition)
        {
            for (int i = 0; i < countRetry; i++)
            {
                try
                {
                    var elmnt = parent.FindFirst(TreeScope.Subtree, condition);
                    if (elmnt != null)
                    {
                        return elmnt;
                    }
                }
                catch (Exception e)
                {
                    Log.Error(string.Format("用Subtree参数调用FindFirst方法时出现异常,异常消息:{0}", e.Message));

                }
                Thread.Sleep(interval);
            }
            return null;
        }

        /// <summary>
        /// 通过父节点查找所有的子节点中的所有满足条件的子节点
        /// </summary>
        /// <param name="countRetry">重试次数.</param>
        /// <param name="interval">时间间隔</param>
        /// <param name="parent">父节点.</param>
        /// <param name="condition">条件</param>
        /// <returns></returns>
        public static AutomationElementCollection FindAllOfSubtree(int countRetry, int interval, AutomationElement parent, Condition condition)
        {
            for (int i = 0; i < countRetry; i++)
            {
                try
                {
                    var elmnt = parent.FindAll(TreeScope.Subtree, condition);
                    if (elmnt != null)
                    {
                        return elmnt;
                    }
                }
                catch (Exception e)
                {
                    Log.Error(string.Format("用Subtree参数调用FindAll方法时出现异常,异常消息:{0}", e.Message));
                }
                Thread.Sleep(interval);
            }
            return null;
        }
        #endregion 查找界面元素

        /// <summary>
        /// 设置编辑框的值
        /// </summary>
        /// <param name="edit"></param>
        /// <param name="val"></param>
        public static void SetValue(AutomationElement edit,string val)
        {
            var pattern = (ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern);
            pattern.SetValue(val);
        }

        /// <summary>
        /// 点击指定位置
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        public static void Click(int x,int y)
        {
            Point point = new Point();
            GetCursorPos(ref point);
            SetCursorPos(x, y);
            Thread.Sleep(200);
            mouse_event(2, x, y, 0, IntPtr.Zero);
            mouse_event(4, x, y, 0, IntPtr.Zero);
            Thread.Sleep(300);
            SetCursorPos((int)point.X, (int)point.Y);
        }


        private static T Retry<T>(int count,int interval,
            Func<TreeScope,Condition,T> func,TreeScope treeScope,Condition condition)
        {
            for (int i = 0; i < count; i++)
            {
                try
                {
                    T elmnt = func(treeScope, condition);
                    if (elmnt != null)
                    {
                        return elmnt ;
                    }
                }
                catch (Exception e)
                {
                    Log.Error(string.Format("用Children参数调用FindFirst方法时出现异常,异常消息:{0}", e.Message));
                }
                Thread.Sleep(interval);
            }
            return default(T);
        }


        #region Win32
        [DllImport("user32.dll")]
        private static extern void mouse_event(uint dwFlags, int dX, int dY, uint dwData, IntPtr dwExtraInfo);
        [DllImport("user32.dll")]
        private static extern void SetCursorPos(int dX, int dY);
        [DllImport("user32.dll")]
        private static extern void GetCursorPos(ref Point point);
        //置顶窗体
        [DllImport("user32.dll", CharSet = CharSet.None, ExactSpelling = false)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        #endregion Win32

    }
}
 

4、使用

        /// <summary>
        /// 多企业管理窗口
        /// </summary>
        protected AutomationElement ElmntCompanyManager
        {
            get
            {
                return UIAutoHelper.FindFirstOfChildren(10, 200, UIAutoHelper.ElmntRoot,
                    UIAutoHelper.GetConditionFromArray(new PropertyCondition[]
                    {
                        UIAutoHelper.GetConditionWithClassName("TFrmCompanyMgrView"),
                        UIAutoHelper.GetConditionWithName("多企业管理")
                    }
                    ));
            }
        }

       protected AutomationElement EditTaxpayer
        {
            get
            {
                var elmnt = ElmntCompanyManager;
                var edits = UIAutoHelper.FindAllOfSubtree(10, 100, elmnt, UIAutoHelper.GetConditionFromArray(new PropertyCondition[]
                {
                    UIAutoHelper.GetConditionWithProcessId(elmnt.Current.ProcessId),
                    UIAutoHelper.GetConditionWithClassName("TITSEdit"),
                }));
                if (edits != null && edits.Count > 0)
                {
                    return edits[0];
                }
                return null;
            }
        }

 

<think>好的,用户现在的问题是想在Windows系统中使用UIAutomation实现拼多多工作台的自动化操作。之前已经提供过一些方案,比如使用Selenium或PyAutoGUI,但用户现在特别提到UIAutomation,可能是指微软的UI Automation框架。我需要结合之前的回答和用户的新需求,给出具体的实现步骤。 首先,用户之前的回复中引用了RPA、机器学习和网站设计流程的相关内容,但当前的问题更偏向于技术实现。需要确认用户是否需要详细的代码示例,或者更侧重于步骤和工具的选择。 用户提到的是Windows环境,所以可能需要使用专门针对Windows GUI自动化的工具,比如Python的UIAutomation库或者PyWinAuto。之前的回答中提到了Selenium,但那是针对Web的,而拼多多工作台如果是桌面应用的话,可能需要不同的方法。 需要检查之前的引用是否有相关技术,比如引用[1]中的RPA工具如UiPath可能适用,但用户希望自己实现,而不是用现成的RPA平台。因此,可能需要推荐Python库,比如pywinauto或uiautomation。 另外,用户之前的问题中提到了处理安全校验和验证码,可能需要结合机器学习,如引用[2]中的内容,但当前问题可能更关注基础操作。 需要确保步骤清晰,包括环境搭建、元素定位、操作模拟,以及异常处理。同时,用户可能需要知道如何获取元素信息,比如使用Inspect.exe工具。 还需要考虑权限问题,比如以管理员身份运行脚本,以及可能的反自动化措施,比如拼多多工作台是否有检测自动化的机制,需要如何处理。 最后,生成相关问题时要针对可能遇到的后续问题,比如元素定位困难、验证码处理、提升稳定性等。</think>在Windows系统中使用UIAutomation实现拼多多工作台的自动化操作,可以按照以下技术方案分步实施: --- ### 一、技术选型建议 1. **Python + `uiautomation`库** - 微软原生UI Automation框架的Python封装,支持Windows GUI元素识别[^1] - 示例安装命令: ```python pip install uiautomation ``` 2. **辅助工具** - **Inspect.exe**(Windows SDK自带):定位控件元素属性 - **Accessibility Insights**:可视化分析UI层级结构 --- ### 二、核心实现步骤 #### 步骤1:环境准备 ```python import uiautomation as auto auto.SetGlobalSearchTimeout(15) # 设置全局元素查找超时时间 ``` #### 步骤2:启动应用程序 ```python # 通过进程名启动(需提前获取拼多多工作台exe名称) app = auto.WindowControl(searchDepth=1, Name="拼多多工作台") app.SetActive() # 激活窗口 ``` #### 步骤3:元素定位(以登录为例) ```python # 定位用户名输入框(使用Inspect.exe获取元素属性) username = app.EditControl(Name="账号输入框", AutomationId="tbUsername") username.SendKeys("your_account") # 定位密码输入框 password = app.EditControl(Name="密码输入框", ClassName="Edit") password.SendKeys("your_password{ENTER}") # 输入后自动回车 ``` #### 步骤4:复杂操作示例(订单导出) ```python # 定位订单管理菜单 order_menu = app.MenuItemControl(Name="订单管理") order_menu.Click() # 定位导出按钮(可能需要处理延迟加载) export_btn = auto.WaitForExist(lambda: app.ButtonControl(Name="导出"), timeout=10) export_btn.Click() # 处理文件保存对话框 save_dialog = auto.WindowControl(Name="另存为") save_dialog.EditControl(Name="文件名:").SendKeys("orders_202310.csv") save_dialog.ButtonControl(Name="保存(S)").Click() ``` --- ### 三、关键问题解决方案 1. **动态元素定位** ```python # 使用正则表达式匹配部分名称 dynamic_element = app.PaneControl(searchDepth=3, Name=auto.RegexPattern(".*今日订单.*")) ``` 2. **异步加载等待** ```python auto.WaitForExist(lambda: app.ListItemControl(Name="数据加载完成"), timeout=20) ``` 3. **验证码处理** - 方案一:接入第三方OCR服务(需考虑合规性) ```python captcha_img = app.ImageControl(AutomationId="captchaImage") captcha_img.CaptureToImage("temp.png") code = call_ocr_api("temp.png") # 调用OCR接口 ``` - 方案二:设置手动断点 ```python auto.Logger.WriteLine("请手动输入验证码后继续...") input("按回车继续执行") ``` --- ### 四、优化建议 1. **异常处理增强** ```python try: element.Click() except auto.ElementNotFoundError: auto.Logger.Error("元素定位失败,尝试备用方案") fallback_element.Click() ``` 2. **操作日志记录** ```python auto.Logger.Write("开始执行订单导出流程", consoleColor='green') ``` 3. **权限配置** - 右键使用**管理员身份运行**Python脚本 - 关闭拼多多工作台的DPI缩放适配 --- ### 五、典型代码结构 ```python from uiautomation import * import time def pdd_automation(): try: # 启动应用 app = WindowControl(Name="拼多多工作台") app.SwitchToThisWindow() # 执行核心业务流程 perform_login(app) export_orders(app) except Exception as e: Logger.Error(f"自动化执行失败: {str(e)}") def perform_login(window): window.EditControl(Name="账号输入框").SendKeys("test@example.com") window.EditControl(Name="密码输入框").SendKeys("password123{ENTER}") time.sleep(3) # 等待登录完成 if __name__ == "__main__": pdd_automation() ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值