.net - 需要拍摄事件 chrome 窗口的全页快照

标签 .net wpf google-chrome

单击 WPF 应用程序中的按钮时,我需要拍摄事件 chrome 窗口的全页快照 我使用了下面的代码。但它只给出事件窗口的 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 中捕获完整的网页。

最佳答案

我无法使用其 FindAll 方法找到所有 _AutomationElement_。不管怎样,这可能对你有帮助:

        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;
        }
    }

关于.net - 需要拍摄事件 chrome 窗口的全页快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42197242/

相关文章:

c# - 在 C# 中应该如何测试变量是否为值类型?

c# - 如何在 C# 中向图标添加文本?

.NET GridView 更新不会将 UserID 发送到 SQL 查询

c# - 当类实现 Equals 和 GetHashCode 时,如何使用对象引用作为 C# 字典中的键?

c# - OnPropertyChanged 导致大量的 Action 分配

html - Chrome 和 Firefox 很快就会支持指针事件(压力、倾斜、触摸等)吗?

google-chrome - 强制 Chrome 打印设置

.net - 给定缩放级别,将long/lat转换为像素x/y

css - 为什么 chrome 会删除错误的 css 类?

wpf - Visual Studio 2010 属性帮助