2017-02-13 59 views
3

點擊一個WPF應用程序中的按鈕,我需要採取活動鉻窗口的全頁快照 我已經使用了下面的代碼。但它只給出活動窗口的URL。需要採取活動鉻窗口的全頁快照

Process[] procsChrome = Process.GetProcessesByName("chrome"); 
foreach (Process chrome in procsChrome) 
{ 
// the chrome process must have a window 
if (chrome.MainWindowHandle == IntPtr.Zero) 
{ 
continue; 
} 

// find the automation element 
AutomationElement elm = AutomationElement.FromHandle(chrome.MainWindowHandle); 
AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants, 
new PropertyCondition(AutomationElement.NameProperty, "Address and search bar")); 

// if it can be found, get the value from the URL bar 
if (elmUrlBar != null) 
{ 
AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns(); 
if (patterns.Length > 0) 
{ 
ValuePattern val = ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]); 

Console.WriteLine("Chrome URL found: " + val.Current.Value); 
} 
} 
} 

我需要在chrome中捕捉完整的網頁。

回答

0

我找不到所有_AutomationElement_s使用其FindAll方法。無論如何,這可以幫助你:

其中
 Process[] procsChrome = Process.GetProcessesByName("chrome"); 
     foreach (Process chrome in procsChrome) 
     { 
      if (chrome.MainWindowHandle == IntPtr.Zero) 
       continue; 

      AutomationElement rootElement = AutomationElement.FromHandle(chrome.MainWindowHandle); 
      foreach (AutomationElement v in FindInRawView(rootElement)) 
      { 
       Console.WriteLine(GetText(v)); 
      } 
      AutomationElement elmUrlBar = rootElement.FindFirst(TreeScope.Descendants, 
      new PropertyCondition(AutomationElement.NameProperty, "Address and search bar")); 
     } 

this answer用於:

public static IEnumerable<AutomationElement> FindInRawView(AutomationElement root) 
    { 
     TreeWalker rawViewWalker = TreeWalker.RawViewWalker; 
     Queue<AutomationElement> queue = new Queue<AutomationElement>(); 
     queue.Enqueue(root); 
     while (queue.Count > 0) 
     { 
      var element = queue.Dequeue(); 
      yield return element; 

      var sibling = rawViewWalker.GetNextSibling(element); 
      if (sibling != null) 
      { 
       queue.Enqueue(sibling); 
      } 

      var child = rawViewWalker.GetFirstChild(element); 
      if (child != null) 
      { 
       queue.Enqueue(child); 
      } 
     } 
    } 

this answer

public static string GetText(AutomationElement element) 
    { 
     object patternObj; 
     if (element.TryGetCurrentPattern(ValuePattern.Pattern, out patternObj)) 
     { 
      var valuePattern = (ValuePattern)patternObj; 
      return valuePattern.Current.Value; 
     } 
     else if (element.TryGetCurrentPattern(TextPattern.Pattern, out patternObj)) 
     { 
      var textPattern = (TextPattern)patternObj; 
      return textPattern.DocumentRange.GetText(-1).TrimEnd('\r'); // often there is an extra '\r' hanging off the end. 
     } 
     else 
     { 
      return element.Current.Name; 
     } 
    }