Smile

Smile

  • 1.4k
  • 198
  • 33.9k

How to all the urls of the open tabs of a browser

Feb 7 2020 6:48 AM
I am trying to read all the urls of all the tabs of a browser i.e. google chrome.By the below code i am able to get only the active tab url.But i want all the urls of the tabs of a browser.
 
below is the code
  1. System.Diagnostics.Process[] procsChrome = System.Diagnostics.Process.GetProcessesByName("CHROME");    
  2. foreach (Process proc in procsChrome)    
  3. {    
  4. string procname = proc.ProcessName;    
  5. // the chrome process must have a window    
  6. if (proc.MainWindowHandle == IntPtr.Zero)    
  7. continue;    
  8. // to find the tabs we first need to locate something reliable - the 'New Tab' button    
  9. AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);    
  10. var SearchBar = root.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));    
  11. if (SearchBar != null)    
  12. {    
  13. string str = (string)SearchBar.GetCurrentPropertyValue(ValuePatternIdentifiers.ValueProperty);    
  14. }    
  15. } 
Even i tried below one also
  1. private string GetUrl(BrowserType browser)  
  2. {  
  3. if (browser == BrowserType.Chrome)  
  4. {  
  5. //"Chrome_WidgetWin_1"  
  6. Process[] procsChrome = Process.GetProcessesByName("chrome");  
  7. foreach (Process chrome in procsChrome)  
  8. {  
  9. // the chrome process must have a window  
  10. if (chrome.MainWindowHandle == IntPtr.Zero)  
  11. {  
  12. continue;  
  13. }  
  14. //AutomationElement elm = AutomationElement.RootElement.FindFirst(TreeScope.Children,  
  15. // new PropertyCondition(AutomationElement.ClassNameProperty, "Chrome_WidgetWin_1"));  
  16. // find the automation element  
  17. AutomationElement elm = AutomationElement.FromHandle(chrome.MainWindowHandle);  
  18. // manually walk through the tree, searching using TreeScope.Descendants is too slow (even if it's more reliable)  
  19. AutomationElement elmUrlBar = null;  
  20. try  
  21. {  
  22. // walking path found using inspect.exe (Windows SDK) for Chrome 29.0.1547.76 m (currently the latest stable)  
  23. var elm1 = elm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));  
  24. var elm2 = TreeWalker.ControlViewWalker.GetLastChild(elm1); // I don't know a Condition for this for finding :(  
  25. var elm3 = elm2.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""));  
  26. var elm4 = elm3.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar));  
  27. elmUrlBar = elm1.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));  
  28. }  
  29. catch(Exception ex)  
  30. {  
  31. ex.ToString();  
  32. // Chrome has probably changed something, and above walking needs to be modified. :(  
  33. // put an assertion here or something to make sure you don't miss it  
  34. continue;  
  35. }  
  36. // make sure it's valid  
  37. if (elmUrlBar == null)  
  38. {  
  39. // it's not..  
  40. continue;  
  41. }  
  42. // 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  
  43. if ((bool)elmUrlBar.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty))  
  44. {  
  45. continue;  
  46. }  
  47. // there might not be a valid pattern to use, so we have to make sure we have one  
  48. AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();  
  49. if (patterns.Length == 1)  
  50. {  
  51. string ret = "";  
  52. try  
  53. {  
  54. ret = ((ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0])).Current.Value;  
  55. }  
  56. catch { }  
  57. if (ret != "")  
  58. {  
  59. // must match a domain name (and possibly "https://" in front)  
  60. if (Regex.IsMatch(ret, @"^(https:\/\/)?[a-zA-Z0-9\-\.]+(\.[a-zA-Z]{2,4}).*$"))  
  61. {  
  62. // prepend http:// to the url, because Chrome hides it if it's not SSL  
  63. if (!ret.StartsWith("http"))  
  64. {  
  65. ret = "http://" + ret;  
  66. }  
  67. return ret;  
  68. }  
  69. }  
  70. continue;  
  71. }  
  72. }  
  73. }  
  74. return null;  
  75. }  
The line high lighted is throwing exeception object refernce is null

Answers (1)