このコードは動作しているはずです。少なくとも他の人は同じですが、同じエラーが発生しますArgumentNullException was unhandled
エラー?何が問題なの?Chromeの開いているタブを印刷しようとするとSystem.Windows.AutomationでArgumentNullExceptionが発生する
Google Chromeの開いたタブを印刷することになっています。アプリを実行して、あなたに適しているかどうかを確認してください。
私はこのオートメーション事の専門家ではないんだけど、ここで私は、ウィンドウ内のすべてのボタンを探し、それがあることが判明し
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Automation;
namespace ConsoleApplication3
{
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 condTabs = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button);
//Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "new tab");
AutomationElementCollection col = root.FindAll(TreeScope.Descendants, condTabs);
AutomationElement elmNewTab = col[4]; // allways the position 4 is new tab button
// get the tabstrip by getting the parent of the 'new tab' button
TreeWalker treewalker = TreeWalker.ControlViewWalker;
AutomationElement elmTabStrip = treewalker.GetParent(elmNewTab); // <- Error on this line
// 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.ReadKey();
}
}
}
}
}
を働いてUIAtomationClient.dll
とUIAutomationTypes.dll
using System.Windows.Automation;
...
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 on this line
// 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);
}
}
}
それをテストし、あなたは 'elmNewTab'がnullである理由を確認するためにデバッグする必要があります。 (ArgumentNullException) –
投票がなぜ失敗するのですか?私はこの質問が悪い質問であるとは思わない。あなたはdownvoters説明してくださいできますか? – r1verside
@ r1verside私は自分自身で質問を落としていませんでしたが、タイトルは「コードはうまくいくはずですが、それはありません」と私がここで見ている最悪のものの1つです。私は新しいものを提案しました、それは今編集キューにあります。うまくいけば誰かがそれを承認するでしょう。質問自体は私にとっては良さそうだ。 –