C#在活动标签上获取Chrome网址

时间:2015-11-05 08:12:53

标签: c# google-chrome

现在我正在使用以下代码从活动标签i chrome浏览器

获取网址
    /*
     * 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;
    }

此代码可作为魅力,但从chrome读取此URL之间存在延迟。获取此URL需要2到4秒。 有谁知道为什么? 感谢任何线索...

1 个答案:

答案 0 :(得分:0)

我有七个进程chrome.exe在运行。我已经使用秒表检查了我的代码中的延迟,并且我发现了导致此延迟的一段代码

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

处理此行需要1450毫秒

相关问题