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
- System.Diagnostics.Process[] procsChrome = System.Diagnostics.Process.GetProcessesByName("CHROME");
- foreach (Process proc in procsChrome)
- {
- string procname = proc.ProcessName;
-
- if (proc.MainWindowHandle == IntPtr.Zero)
- continue;
-
- AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
- var SearchBar = root.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
- if (SearchBar != null)
- {
- string str = (string)SearchBar.GetCurrentPropertyValue(ValuePatternIdentifiers.ValueProperty);
- }
- }
Even i tried below one also
- private string GetUrl(BrowserType browser)
- {
- if (browser == BrowserType.Chrome)
- {
-
- Process[] procsChrome = Process.GetProcessesByName("chrome");
- foreach (Process chrome in procsChrome)
- {
-
- if (chrome.MainWindowHandle == IntPtr.Zero)
- {
- continue;
- }
-
-
-
- AutomationElement elm = AutomationElement.FromHandle(chrome.MainWindowHandle);
-
- AutomationElement elmUrlBar = null;
- try
- {
-
- var elm1 = elm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));
- var elm2 = TreeWalker.ControlViewWalker.GetLastChild(elm1);
- var elm3 = elm2.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""));
- var elm4 = elm3.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar));
- elmUrlBar = elm1.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
- }
- catch(Exception ex)
- {
- ex.ToString();
-
-
- continue;
- }
-
- if (elmUrlBar == null)
- {
-
- continue;
- }
-
- if ((bool)elmUrlBar.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty))
- {
- continue;
- }
-
- AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
- if (patterns.Length == 1)
- {
- string ret = "";
- try
- {
- ret = ((ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0])).Current.Value;
- }
- catch { }
- if (ret != "")
- {
-
- if (Regex.IsMatch(ret, @"^(https:\/\/)?[a-zA-Z0-9\-\.]+(\.[a-zA-Z]{2,4}).*$"))
- {
-
- if (!ret.StartsWith("http"))
- {
- ret = "http://" + ret;
- }
- return ret;
- }
- }
- continue;
- }
- }
- }
- return null;
- }
The line high lighted is throwing exeception object refernce is null