【问题标题】:Error "Value can't be null", UIAutomationElement错误“值不能为空”,UIAutomationElement
【发布时间】:2017-12-16 21:43:21
【问题描述】:

所以我试图在 C# 中使用 UIAutomation 从 Chrome 中获取所有打开的标签,但我不断收到错误消息:

发生System.ArgumentNullException

HResult=0x80004003
Message=Value 不能为 NULL。
Source=UIAutomationClient

堆栈跟踪:
在 System.Windows.Automation.TreeWalker.GetParent(AutomationElement 元素)
在 chromeTabsTest.Program.Main(String[] args) 在 C:\Users...\chromeTabsTest\chromeTabsTest\Program.cs:line 31

错误在代码中用注释指示。

    using System;
using System.Diagnostics;
using System.Windows.Automation;

namespace chromeTabsTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Process[] procsChrome = Process.GetProcessesByName("chrome");
            if (procsChrome.Length <= 0)
            {
                Console.WriteLine("Chrome is not running");
            }
            else
            {
                foreach (Process proc in procsChrome)
                {
                    // the chrome process must have a window 
                    if (proc.MainWindowHandle == IntPtr.Zero)
                    {
                        continue;
                    }
                    // to find the tabs we first need to locate something reliable - the 'New Tab' button 
                    AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
                    Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "New Tab");
                    AutomationElement elmNewTab = root.FindFirst(TreeScope.Descendants, condNewTab);
                    // get the tabstrip by getting the parent of the 'new tab' button 
                    TreeWalker treewalker = TreeWalker.ControlViewWalker;
                    AutomationElement elmTabStrip = treewalker.GetParent(elmNewTab); // <------------- Error here 
                    // loop through all the tabs and get the names which is the page title 
                    Condition condTabItem = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
                    foreach (AutomationElement tabitem in elmTabStrip.FindAll(TreeScope.Children, condTabItem))
                    {
                        Console.WriteLine(tabitem.Current.Name);
                    }
                }
            }


            Console.Write("\nPress any key to continue...");
            Console.ReadKey();
        }
    }
}

此代码来自另一个 Stack Overflow 问题:question

【问题讨论】:

  • 你在哪一行得到错误?
  • 您的标题说您的错误是“值不能为空” - 您的问题是“值不能为负数”。这似乎是两种截然不同的东西。是哪一个?
  • 没有人有时间在没有任何建议的情况下分析您的代码,在哪一行抛出错误..
  • exact 错误消息是什么(请复制并粘贴它,因为您在输入时似乎无法正确理解)?哪条线引发了异常? (堆栈跟踪会告诉您这一点,您也可以通过调试器中的代码单步执行来找到它。)
  • 问题已编辑我解决了所有的 cmets。谢谢大家指出主题。

标签: c# google-chrome ui-automation


【解决方案1】:

以下行似乎是语言敏感的:

Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "New Tab");

也就是说,“新标签”不是一个内部字段,而是一个本地化的字符串。这意味着必须更新此行以具有此文本的正确本地化版本。

很有可能有更好的“找到可靠的东西”可以使用,但我对 chrome 自动化不够熟悉,无法说出是否有,如果有的话。

【讨论】:

    猜你喜欢
    • 2015-11-07
    • 2014-01-30
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 2020-11-16
    • 2011-12-08
    相关资源
    最近更新 更多