Je suis en train de gett tous les onglets ouverts de Chrome à l'aide UIAutomation en C#, mais je continue à obtenir l'erreur:Erreur « La valeur ne peut être nulle », UIAutomationElement
System.ArgumentNullException occurred
HResult=0x80004003
Message=Value can't be NULL.
Source=UIAutomationClientStackTrace:
at System.Windows.Automation.TreeWalker.GetParent(AutomationElement element)
at chromeTabsTest.Program.Main(String[] args) in C:\Users...\chromeTabsTest\chromeTabsTest\Program.cs:line 31
L'erreur est indiquée par un commenter dans le code.
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();
}
}
}
Ce code est d'un autre débordement de la pile question: question
Sur quelle ligne rencontrez-vous l'erreur? –
Votre titre indique que votre erreur est "La valeur ne peut pas être nulle" - votre question dit "La valeur ne peut pas être négative". Ceux-ci semblent être deux choses très différentes. Laquelle est-ce? – Chris
Personne n'a le temps d'analyser votre code sans aucune suggestion, dans quelle ligne l'erreur est renvoyée .. –