【发布时间】: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