如何在C#中从Google Chrome版本45获取当前URL?

3

提供的搜索结果有什么问题?请详细说明您的问题。 - Mauricio Arias Olave
我的之前的搜索在之前的版本中正常工作。 - Abdalmohaymen Alesmaeel
你应该尝试在你的问题中添加一些代码示例。阅读Stack Overflow的导览,并使用你提供的搜索结果来更新你的问题。这些更改将有助于你获得恰当的答案。 - Mauricio Arias Olave
是的,自Chrome 38或40以来,有些东西已经改变了。 - Yves Schelpe
我制作了一个谷歌浏览器插件来解决这个问题。 - Abdalmohaymen Alesmaeel
3个回答

2

从版本38开始尝试使用此方法,直至45.x(撰写本文时)。

public string GetChormeURL(string ProcessName)
        {
            string ret = "";
            Process[] procs = Process.GetProcessesByName(ProcessName);
            foreach (Process proc in procs)
            {
                // the chrome process must have a window
                if (proc.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(proc.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 43.0.2357.81 m (currently the latest stable)
                // Inspect.exe path - C://Program files (X86)/Windows Kits/10/bin/x64
                var elm1 = elm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));
                if (elm1 == null) { continue; } // not the right chrome.exe
                var elm2 = TreeWalker.RawViewWalker.GetLastChild(elm1); // I don't know a Condition for this for finding
                var elm3 = elm2.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""));
                var elm4 = TreeWalker.RawViewWalker.GetNextSibling(elm3); // I don't know a Condition for this for finding
                var elm5 = elm4.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar));
                var elm6 = elm5.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""));
                elmUrlBar = elm6.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
            }
            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)
            {
                try
                {
                    ret = ((ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0])).Current.Value;
                    return ret;
                }
                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;
            }
        }
        return ret;
    }

来源: http://www.codeproject.com/Answers/995898/how-to-get-URL-form-any-web-browser-like-firefox-i

如何从任何网页浏览器(例如Firefox或Internet Explorer)获取URL?

我想要一个可以在C#中使用的代码示例,该代码示例可以从当前打开的任何网络浏览器中获取网址并将其存储在字符串变量中。


现在我正在努力解决这个问题。我决定创建一个Chrome扩展程序来实现相同的功能。 - Abdalmohaymen Alesmaeel
你在哪里失败了?我已经将它插入我的项目中(当然进行了适配),并且它可以很好地检测到Chrome 45。你使用的是什么系统? - Yves Schelpe
非常抱歉,我的系统是Windows 7。 - Abdalmohaymen Alesmaeel
应该也能在Windows 7上运行-因为已经在那里测试过了。请注意,它可能不再适用于Chrome 45以上的版本..这些事情变化很快。我已经为此实现了策略模式,并根据浏览器的版本触发特定的算法(策略)。 - Yves Schelpe

1
尝试这个。
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);
                    }
                }
            }

0
请注意,通过名称“地址和搜索栏”获取元素是本地化的,因此仅适用于英语Chrome / Windows。使用inspect.exe获取正确的本地化名称。对于德语,它是:“Adress- und Suchleiste”。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接