c# Getting Chrome URL's from all tab

后端 未结 3 939
北海茫月
北海茫月 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:49

    I solved the same problem recently with System.Windows.Forms.SendKeys Compared to your result - it's 4 times faster, but doesn't work with websites which use ctrl+l hotkey (for example StackOwerflow in edit mode). Depends on your needs :)

     public void WithSendkeys()
        {
            AutomationElement.RootElement
                .FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "Chrome_WidgetWin_1"))
                .SetFocus();
            SendKeys.SendWait("^l");
            var elmUrlBar = AutomationElement.FocusedElement;
            var valuePattern = (ValuePattern) elmUrlBar.GetCurrentPattern(ValuePattern.Pattern);
            Console.WriteLine(valuePattern.Current.Value);
        }
    
    0 讨论(0)
  • 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);
                        }
                    }
                }
    
    0 讨论(0)
  • 2021-01-16 06:58

    Here is the code:

    AutomationElement root = AutomationElement.FromHandle(proc.MainWindowHandle);
    Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "Novo separador");
    AutomationElement elmNewTab = root.FindFirst(TreeScope.Descendants, condNewTab);
    TreeWalker treewalker = TreeWalker.ControlViewWalker;
    
    // IF THROWS A ERROR HERE, LOOK AT THE AutomationElement.NameProperty ("Novo separador") - PUT THIS TEXT IN APROPRIATE LANGUAGE
    AutomationElement elmTabStrip = treewalker.GetParent(elmNewTab);
                                                                                
    Condition condTabItem = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
    foreach (AutomationElement tabitem in elmTabStrip.FindAll(TreeScope.Children, condTabItem))
    {
        // HERE IS YOUR TABS!!!!
        ret.Add(tabitem.Current.Name);
    }
    
    0 讨论(0)
提交回复
热议问题