【问题标题】:Save button is disabled on edit form, cannot click it via UIAutomation编辑表单上的保存按钮被禁用,无法通过 UIAutomation 单击它
【发布时间】:2014-09-29 19:28:43
【问题描述】:

这个问题和我的last question在同一个应用程序中。

我遇到了一些“奇怪”的行为,我不知道它是否按设计运行,或者是否有问题。在我“注入”数据的第 3 方应用程序中,当我手动编辑记录与自动应用程序编辑记录时,我会遇到不同的行为。

如果我运行第 3 方应用程序并手动编辑记录,一旦我开始输入字段,工具栏中的“保存”和“撤消”按钮就会启用。

如果我运行第 3 方应用程序,然后运行我的自动化应用程序来编辑记录,我的应用程序将焦点设置到表单上的第一个字段,然后将数据“注入”到字段中(实际上看起来像是有人在输入它非常快)但“保存”和“撤消”按钮一直处于禁用状态。当我到达表单底部时,我尝试调用“保存”按钮,但收到错误消息:

"在 UIAutomationClient.dll 中发生了“System.Windows.Automation.ElementNotEnabledException”类型的未处理异常 附加信息:不允许对未启用的元素执行该操作。"

我在 MSDN 上使用 this code example 在 3rd 方应用程序的文本框中插入文本。

if (!element.TryGetCurrentPattern(
      ValuePattern.Pattern, out valuePattern))
  {
    // Set focus for input functionality and begin.
    element.SetFocus();

    // Pause before sending keyboard input.
    Thread.Sleep(100);

    // Delete existing content in the control and insert new content.
    SendKeys.SendWait("^{HOME}");   // Move to start of control
    SendKeys.SendWait("^+{END}");   // Select everything
    SendKeys.SendWait("{DEL}");     // Delete selection
    SendKeys.SendWait(value);
  }
  // Control supports the ValuePattern pattern so we can  
  // use the SetValue method to insert content. 
  else
  {
    // Set focus for input functionality and begin.
    element.SetFocus();

    ((ValuePattern)valuePattern).SetValue(value);
  }

也许我没有搜索正确的关键字,但谷歌对我的帮助不大,我只找到了一个似乎相关的SO post。如果有人能对此有所了解,我将不胜感激。

TIA


更新:

Re:为什么不检查按钮是否启用...

我不知道如何做到这一点,我今天早些时候试图做到这一点。我有一个引用“保存”按钮的 AutomationElement,但 AutomationElement 没有 Enabled 属性。

AutomationElement toolbar = _mainWindow.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "59392"));
AutomationElement saveButton = toolbar.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Commit Changes (CTRL S)"));

我尝试将 AutomationElement 转换为 Button 以检查 Enabled 属性,但我收到构建错误“无法将类型 'System.Windows.Automation.AutomationElement' 转换为 'System.Windows.Forms.Button' em>"

if (!((Button)saveButton).Enabled)
    ((Button)saveButton).Enabled = true;

...好的,更多搜索导致:UIAutomation Button Style Enabled 所以现在我可以检查它是否已启用,但我还没有弄清楚如何启用它。

2014.10.02 - 我认为无法通过 UIAutomation 启用禁用的按钮。所以我会稍微修改一下我的问题。如果我手动单击输入字段并开始键入“保存”按钮,则会启用。如果我使用 UIAutomation 修改记录,则不会启用“保存”按钮。那么,如何使用 UIAutomation 使窗口进入与手动编辑记录时相同的状态?

【问题讨论】:

  • 为什么不检查它是否已启用,如果没有启用,因为您已经引用了相关元素。
  • @Sorceri 我用你的问题的答案更新了我的问题

标签: c#-4.0 ui-automation


【解决方案1】:

您可以尝试使用 win32 api 直接向按钮发送消息。您可以从 uiautomation 中获取所需的所有句柄,只需使用像这样的 post message 功能。

    public TestSetup AutomationLibrary;

    [DllImport("User32.dll")] //http://msdn.microsoft.com/en-us/library/windows/desktop/ms644944(v=vs.85).aspx
    public static extern int PostMessage(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam);

    public int WM_ENABLE = 0x0A;
    public int WM_COMMAND = 0x111;

    public MainWindow()
    {
        InitializeComponent();

        AutomationLibrary = new TestSetup();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        AutomationElement aeDesktop = AutomationLibrary.GetDesktop();
        AutomationElement aeForm1 = AutomationLibrary.GetAutomationElement("Form1", aeDesktop);
        AutomationElement aeDisabledButton = AutomationLibrary.GetAutomationElement("DISABLED", aeForm1);

        IntPtr windowIntPtr = new IntPtr(aeForm1.Current.NativeWindowHandle);
        IntPtr controlIntPtr = new IntPtr(aeDisabledButton.Current.NativeWindowHandle);

        PostMessage(windowIntPtr, WM_COMMAND, (IntPtr)WM_ENABLE, controlIntPtr);
    }

TestSetup 是我自己自制的自动化库,但基本上我只是获取自动化元素,然后使用 User32.dll 中的 PostMessage 函数来启用按钮。虽然它似乎并没有真正启用按钮,但它确实有点击按钮的意外后果。

我用这篇文章作为postmessage函数http://www.codeproject.com/Articles/487938/Re-Active-Disabled-Controls的参考

希望对你有帮助

【讨论】:

  • 感谢您的回复。不幸的是,我没有时间尝试您的建议,最终我使用的 UI 自动化比我想要的少,而 SendKeys.Send() 比我想要的多。出于某种原因,如果我使用 SendKeys 编写所有字段,则按钮没有问题。我没有收到任何回复,而且我有一个截止日期,所以我需要做任何有效的事情。
猜你喜欢
  • 1970-01-01
  • 2021-01-03
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 2018-12-07
相关资源
最近更新 更多