2016-10-16 23 views
-1

このコードは動作しているはずです。少なくとも他の人は同じですが、同じエラーが発生します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.dllUIAutomationTypes.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); 
         } 

        } 
       } 
+1

それをテストし、あなたは 'elmNewTab'がnullである理由を確認するためにデバッグする必要があります。 (ArgumentNullException) –

+0

投票がなぜ失敗するのですか?私はこの質問が悪い質問であるとは思わない。あなたはdownvoters説明してくださいできますか? – r1verside

+0

@ r1verside私は自分自身で質問を落としていませんでしたが、タイトルは「コードはうまくいくはずですが、それはありません」と私がここで見ている最悪のものの1つです。私は新しいものを提案しました、それは今編集キューにあります。うまくいけば誰かがそれを承認するでしょう。質問自体は私にとっては良さそうだ。 –

答えて

0

をインポート新しいタブは、ボタンを最小化、復元、終了する前に、常に位置4にあります。私はのマジックナンバーを組み込む最もクリーンなソリューションではないが、それは仕事をしていることは分かっている。

ここに行ってください!私は例外ではVS2015

working :S

+0

これは何のために印刷されますか? – user6879072

+0

c# - コードは正常に動作するはずですが、そうではありません - スタックオーバーフロー asdf - Google検索 Recibidos - ***@gmail.com - Gmail Facebook - Entra Oregístrate インターネットに接続しています。 Ofertas exclusivas - Telepizza.es fasdfa + - Google検索 これは私がこれをテストするために開いているタブです。 – r1verside

+0

私は何もしません、コンソールは黒で、それはそれです。 – user6879072

関連する問題