Get all opened tab URL's in C# windows forms

700 views Asked by At

I'm trying to get all the url's of google chrome in windows forms with c#. Currently I had tried with an AutomationElement which gives me only active tab url instead of all opened tabs.

My code is like below:

public void GetAllUrls()
{
    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);
                listBox1.Items.Add(val.Current.Value);
            }
        }
    }
}
0

There are 0 answers