c#从所有标签中获取Chrome URL [英] c# Getting Chrome URL's from all tab

查看:172
本文介绍了c#从所有标签中获取Chrome URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想从浏览器中获取网址,而对于Chrome,我使用了这些,并且没有正常工作。获取null异常我认为chrome已经改变了一些东西..在elm4 == null上收到错误。



使用UIAutomation我搜索了更多,所有的例子都不工作...



refrences: - https://stackoverflow.com/a/21799588/5096993
https://social.msdn.microsoft.com/论坛/ vstudio / en-US / 39bf60a8-2bdc-4aa0-96fb-08dca49cdb06 / c-get-all-chrome-urls-opened?forum = csharpgeneral



< pre $ else if(browser == BrowserType.Chrome)
{
//Chrome_WidgetWin_1

Process [] procsChrome = Process。 GetProcessesByName( 铬);
foreach(procsChrome中的Process chrome)
{
// chrome进程必须有一个窗口
if(chrome.MainWindowHandle == IntPtr.Zero)
{
继续;

// AutomationElement elm = AutomationElement.RootElement.FindFirst(TreeScope.Children,
// new PropertyCondition(AutomationElement.ClassNameProperty,Chrome_WidgetWin_1));
//找到自动化元素
AutomationElement elm = AutomationElement.FromHandle(chrome.MainWindowHandle);

//手动遍历树,使用TreeScope.Descendants进行搜索太慢(即使它更可靠)
AutomationElement elmUrlBar = null;
尝试
{
//使用inspect.exe(Windows SDK)为Chrome 29.0.1547.76 m(当前最新的稳定版)找到的散步路径
var elm1 = elm.FindFirst( TreeScope.Children,新的PropertyCondition(AutomationElement.NameProperty,Google Chrome));
var elm2 = TreeWalker.ControlViewWalker.GetLastChild(elm1); //我不知道如何找到这个条件:(
var elm3 = elm2.FindFirst(TreeScope.Children,new PropertyCondition(AutomationElement.NameProperty,));
var elm4 = elm3 .FindFirst(TreeScope.Children,new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.ToolBar));
elmUrlBar = elm4.FindFirst(TreeScope.Children,new PropertyCondition(AutomationElement.NameProperty,Address and search bar));
}
catch
{
// Chrome可能已经改变了某些东西,而上面的步行需要修改。:(
//在这里放置断言或者确保你不会错过它
continue;
}

//确定它是有效的
if(elmUrlBar == null)
{
//不是..
继续;
}

// elmUrlBar现在是网址栏元素。 ((bool)elmUrlBar.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty))
{
continue;如果我们希望得到一个有效的URL
,我们必须确保它没有键盘焦点。
}

//可能没有可用的有效模式,所以我们必须确保我们有一个
AutomationPattern [] patterns = elmUrlBar.GetSupportedPatterns();
if(patterns.Length == 1)
{
string ret =;
try
{
ret =((ValuePattern)elmUrlBar.GetCurrentPattern(patterns [0]))。Current.Value;
}
catch {}
if(ret!=)
{
//必须匹配域名(也可能是https:// (正面)
if(Regex.IsMatch(ret,@^(https:\ / \ /)?[a-zA-Z0-9\-\。] +(\。[a因为如果它不是SSL,bb会隐藏http $ // b
if //
if (!ret.StartsWith(http))
{
ret =http://+ ret;
}
return ret;
}
}
继续;



$ b


解决方案

这段代码正在为我工​​作,并获得chrome的活动标签的URL

  Process [] procsChrome = Process.GetProcessesByName(chrome); 
foreach(procsChrome中的Process chrome)
{
// chrome进程必须有一个窗口
if(chrome.MainWindowHandle == IntPtr.Zero)
{
继续;
}

//找到自动化元素
AutomationElement elm = AutomationElement.FromHandle(chrome.MainWindowHandle);
AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty,Address and search bar));

//如果可以找到它,从URL栏获得值
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);
listbox.Items.Add(val.Current.Value);
}
}
}


hi i want to get URL from browsers and for chrome i used these and the is not working getting null exception i think chrome has changed something.. getting error on elm4 == null.

using UIAutomation i searched more and all the example are not working ...

refrences:- https://stackoverflow.com/a/21799588/5096993 https://social.msdn.microsoft.com/Forums/vstudio/en-US/39bf60a8-2bdc-4aa0-96fb-08dca49cdb06/c-get-all-chrome-urls-opened?forum=csharpgeneral

else if (browser == BrowserType.Chrome)
            {
                //"Chrome_WidgetWin_1"

                Process[] procsChrome = Process.GetProcessesByName("chrome");
                foreach (Process chrome in procsChrome)
                {
                    // the chrome process must have a window
                    if (chrome.MainWindowHandle == IntPtr.Zero)
                    {
                        continue;
                    }
                    //AutomationElement elm = AutomationElement.RootElement.FindFirst(TreeScope.Children,
                    //         new PropertyCondition(AutomationElement.ClassNameProperty, "Chrome_WidgetWin_1"));
                    // find the automation element
                    AutomationElement elm = AutomationElement.FromHandle(chrome.MainWindowHandle);

                    // manually walk through the tree, searching using TreeScope.Descendants is too slow (even if it's more reliable)
                    AutomationElement elmUrlBar = null;
                    try
                    {
                        // walking path found using inspect.exe (Windows SDK) for Chrome 29.0.1547.76 m (currently the latest stable)
                        var elm1 = elm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));
                        var elm2 = TreeWalker.ControlViewWalker.GetLastChild(elm1); // I don't know a Condition for this for finding :(
                        var elm3 = elm2.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""));
                        var elm4 = elm3.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar));
                        elmUrlBar = elm4.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
                    }
                    catch
                    {
                        // Chrome has probably changed something, and above walking needs to be modified. :(
                        // put an assertion here or something to make sure you don't miss it
                        continue;
                    }

                    // make sure it's valid
                    if (elmUrlBar == null)
                    {
                        // it's not..
                        continue;
                    }

                    // elmUrlBar is now the URL bar element. we have to make sure that it's out of keyboard focus if we want to get a valid URL
                    if ((bool)elmUrlBar.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty))
                    {
                        continue;
                    }

                    // there might not be a valid pattern to use, so we have to make sure we have one
                    AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
                    if (patterns.Length == 1)
                    {
                        string ret = "";
                        try
                        {
                            ret = ((ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0])).Current.Value;
                        }
                        catch { }
                        if (ret != "")
                        {
                            // must match a domain name (and possibly "https://" in front)
                            if (Regex.IsMatch(ret, @"^(https:\/\/)?[a-zA-Z0-9\-\.]+(\.[a-zA-Z]{2,4}).*$"))
                            {
                                // prepend http:// to the url, because Chrome hides it if it's not SSL
                                if (!ret.StartsWith("http"))
                                {
                                    ret = "http://" + ret;
                                }
                                return ret;
                            }
                        }
                        continue;
                    }
                }

            }

解决方案

this code is working for me and get URL of active tab of chrome

 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);
                        listbox.Items.Add(val.Current.Value);
                    }
                }
            }

这篇关于c#从所有标签中获取Chrome URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆