c# Getting Chrome URL's from all tab

后端 未结 3 942
北海茫月
北海茫月 2021-01-16 06:18

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

3条回答
  •  逝去的感伤
    2021-01-16 06:57

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

提交回复
热议问题