0

Right now i'm using this code below to get URL from active tab i chrome browser

    /*
     * GetUrlInternal
     * */
    private string GetUrlInternal(IntPtr process) {
        string sURL = null;
        Process[] procsChrome = Process.GetProcessesByName("chrome");
        foreach (Process chrome in procsChrome) {
            if (chrome.MainWindowHandle == IntPtr.Zero) {
                continue;
            }
            AutomationElement element = AutomationElement.FromHandle(chrome.MainWindowHandle);         
            AutomationElement elm1 = element.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome")); 
            AutomationElement elm2 = TreeWalker.RawViewWalker.GetLastChild(elm1);  
            AutomationElement elm3 = elm2.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""));
            AutomationElement elm4 = elm3.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar));
            AutomationElement elementx = elm1.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Pasek adresu i wyszukiwania"));
            if (!(bool)elementx.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty)) {
                sURL = ((ValuePattern)elementx.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
            }
        }
        return sURL;
    }

This code works as a charm , but there is a delay between read this URL from chrome. It takes from 2 to 4 seconds to get this URL. Does anyone know why? Thanks for any leads...

  • 1
    how many `chrome.exe` processes have you got running? It's probably going through all of them that's why there's a delay. Have you done any sort of analysis of the code? Which lines takes the longest? Is the same line taking the same amount of time in each loop iteration? –  Nov 05 '15 at 08:22
  • Possible duplicate of [Getting the current tab's URL from Google Chrome using C#](http://stackoverflow.com/questions/18897070/getting-the-current-tabs-url-from-google-chrome-using-c-sharp) – mybirthname Nov 05 '15 at 08:23
  • http://stackoverflow.com/questions/18897070/getting-the-current-tabs-url-from-google-chrome-using-c-sharp You can see the EDIT of accepted answer. – mybirthname Nov 05 '15 at 08:24

1 Answers1

0

I've got seven process chrome.exe running. I have checked the delay in my code using stopwatch, and i found a piece of code causing this delay

AutomationElement elementx = elm1.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Pasek adresu i wyszukiwania"));

Processing this line took 1450 ms