Tino
Tino

Reputation: 229

Get active chrome url using C#

I found this solution:

        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);
            AutomationElementCollection elmUrlBars = elm.FindAll(TreeScope.Descendants,
              new PropertyCondition(AutomationElement.NameProperty, ""));

            // if it can be found, get the value from the URL bar
            if (elmUrlBars != null)
            {
                foreach (AutomationElement item in elmUrlBars)
                {
                    AutomationPattern[] patterns = item.GetSupportedPatterns();
                    if (patterns.Length > 0)
                    {
                        ValuePattern val = (ValuePattern)item.GetCurrentPattern(patterns[0]);
                        Console.WriteLine("Chrome URL found: " + val.Current.Value);
                    }
                }
            }
        }
    }

but in is not working in last chrome version (34.0.1847.131 m). Can somebody tell more common solution?

Upvotes: 0

Views: 2933

Answers (2)

Stanley Li
Stanley Li

Reputation: 11

This is an elegant way to get focused window URL for you guys reference.

    public string GetBrowserUrl()
    {
        AutomationElement focusedElement = AutomationElement.FocusedElement;

        if (focusedElement != null)
        {
            object pattern;
            if (focusedElement.TryGetCurrentPattern(ValuePattern.Pattern, out pattern))
            {
                var valuePattern = (ValuePattern)pattern;
                string currentUrl = valuePattern.Current.Value;
                if (!string.IsNullOrEmpty(currentUrl))
                {
                    return currentUrl;
                }
            }
        }

        return null;
    }

Upvotes: 0

Bharat Prajapati
Bharat Prajapati

Reputation: 33

Try this solution in my case it works fine.

string GetChromeUrl(IntPtr hndl)
        {
            if (hndl.ToInt32() > 0)
            {
                string url = "";
                AutomationElement elm = AutomationElement.FromHandle(hndl);
                AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants,
                  new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
                if (elmUrlBar != null)
                {
                    AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
                    if (patterns.Length > 0)
                    {
                        ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]);
                        url = val.Current.Value;
                        return url;
                    }
                }

            }
            return "";
        }

Upvotes: 1

Related Questions