Exemple #1
0
        public static treeelement[] _GetRootElements(Selector anchor)
        {
            var rootelements = new List <treeelement>();

            NMHook.reloadtabs();
            // var tab = NMHook.tabs.Where(x => x.highlighted == true && x.browser == "chrome").FirstOrDefault();
            var tab = NMHook.tabs.Where(x => x.highlighted == true).FirstOrDefault();

            if (tab == null)
            {
                // tab = NMHook.tabs.Where(x => x.browser == "chrome").FirstOrDefault();
                tab = NMHook.tabs.FirstOrDefault();
            }
            if (NMHook.tabs.Count == 0)
            {
                return(rootelements.ToArray());
            }
            // getelement.data = "getdom";
            var getelement = new NativeMessagingMessage("getelement");

            getelement.browser = tab.browser;
            getelement.tabid   = tab.id;
            getelement.xPath   = "/html";
            NativeMessagingMessage result = null;

            try
            {
                result = NMHook.sendMessageResult(getelement, true, TimeSpan.FromSeconds(2));
            }
            catch (Exception)
            {
            }
            if (result != null && result.result != null && result.results == null)
            {
                result.results = new NativeMessagingMessage[] { result };
            }
            if (result != null && result.results != null && result.results.Count() > 0)
            {
                foreach (var res in result.results)
                {
                    if (res.result != null)
                    {
                        //var html = new HtmlElement(getelement.xPath, getelement.cssPath, res.tabid, res.frameId, res.result);
                        var html = new NMElement(res);
                        rootelements.Add(new NMTreeElement(null, true, html));
                    }
                }
                //result = result.results[0];
            }
            return(rootelements.ToArray());
        }
Exemple #2
0
        public static NMElement[] getElement(int tabid, long frameId, string browser, string xPath, TimeSpan timeout)
        {
            var results    = new List <NMElement>();
            var getelement = new NativeMessagingMessage("getelement", PluginConfig.debug_console_output, PluginConfig.unique_xpath_ids);

            getelement.browser = browser;
            getelement.tabid   = tabid;
            getelement.xPath   = xPath;
            getelement.frameId = frameId;
            NativeMessagingMessage result = null;

            try
            {
                result = NMHook.sendMessageResult(getelement, true, timeout);
            }
            catch (Exception)
            {
            }
            if (result != null && result.result != null && result.results == null)
            {
                result.results = new NativeMessagingMessage[] { result };
            }
            if (result != null && result.results != null && result.results.Count() > 0)
            {
                foreach (var res in result.results)
                {
                    if (res.result != null)
                    {
                        //var html = new HtmlElement(getelement.xPath, getelement.cssPath, res.tabid, res.frameId, res.result);
                        res.tab = NMHook.tabs.Where(x => x.id == res.tabid && x.browser == res.browser).FirstOrDefault();
                        var html = new NMElement(res);
                        results.Add(html);
                    }
                }
                //result = result.results[0];
            }
            return(results.ToArray());
        }
Exemple #3
0
        public static treeelement[] _GetRootElements(Selector anchor)
        {
            var rootelements = new List <treeelement>();
            var browser      = "";

            if (NMHook.chromeconnected)
            {
                browser = "chrome";
            }
            if (NMHook.ffconnected)
            {
                browser = "ff";
            }
            if (NMHook.edgeconnected)
            {
                browser = "edge";
            }
            if (string.IsNullOrEmpty(browser))
            {
                return(rootelements.ToArray());
            }
            //NMHook.enumtabs();
            //// var tab = NMHook.tabs.Where(x => x.highlighted == true && x.browser == "chrome").FirstOrDefault();
            //var tab = NMHook.tabs.Where(x => x.highlighted == true).FirstOrDefault();
            //if (tab == null)
            //{
            //    // tab = NMHook.tabs.Where(x => x.browser == "chrome").FirstOrDefault();
            //    tab = NMHook.tabs.FirstOrDefault();
            //}
            //if (NMHook.tabs.Count == 0) { return rootelements.ToArray(); }
            //// getelement.data = "getdom";
            var getelement = new NativeMessagingMessage("getelements", PluginConfig.debug_console_output, PluginConfig.unique_xpath_ids)
            {
                browser = browser,
                xPath   = "/html",
                frameId = -1
            };
            var tab = NMHook.tabs.Where(x => x.browser == browser && (x.active || x.selected)).FirstOrDefault();

            if (tab == null)
            {
                NMHook.enumwindowandtabs();
            }
            if (tab != null)
            {
                getelement.tabid    = tab.id;
                getelement.windowId = tab.windowId;
            }

            if (anchor != null && anchor.Count > 1)
            {
                var s = anchor[1];
                var p = s.Properties.Where(x => x.Name == "xpath").FirstOrDefault();
                if (p != null)
                {
                    getelement.xPath = p.Value;
                }
            }

            NativeMessagingMessage result = null;

            try
            {
                result = NMHook.sendMessageResult(getelement, true, PluginConfig.protocol_timeout);
            }
            catch (Exception)
            {
            }
            if (result != null && result.result != null && result.results == null)
            {
                // result.results = new NativeMessagingMessage[] { result };
                var html = new NMElement(result);
                rootelements.Add(new NMTreeElement(null, true, html));
                return(rootelements.ToArray());
            }
            if (result != null && result.results != null && result.results.Count() > 0)
            {
                foreach (var res in result.results)
                {
                    if (res.result != null)
                    {
                        if (res.tab == null)
                        {
                            res.tab = NMHook.tabs.Where(x => x.browser == res.browser && x.id == res.tabid).FirstOrDefault();
                        }
                        var html = new NMElement(res);
                        rootelements.Add(new NMTreeElement(null, true, html));
                        //var html = new HtmlElement(getelement.xPath, getelement.cssPath, res.tabid, res.frameId, res.result);
                    }
                }
                //result = result.results[0];
            }
            return(rootelements.ToArray());
        }
Exemple #4
0
        //private static NMElement[] GetElementsWithuiSelector(WindowsAccessBridgeInterop.AccessibleJvm jvm, NMSelector selector, IElement fromElement, int maxresults)
        //{
        //    NMElement[] result = null;
        //    NMElement _fromElement = fromElement as NMElement;
        //    var selectors = selector.Where(x => x.Enabled == true && x.Selector == null).ToList();
        //    var current = new List<NMElement>();
        //    NMElement startfrom = null;
        //    if (_fromElement != null) startfrom = _fromElement;
        //    if (startfrom == null) startfrom = new NMElement(jvm);
        //    current.Add(startfrom);
        //    for (var i = 0; i < selectors.Count; i++)
        //    {
        //        var sw = new System.Diagnostics.Stopwatch();
        //        sw.Start();
        //        var s = new NMSelectorItem(selectors[i]);
        //        Log.Selector(string.Format("OpenRPA.NM::GetElementsWithuiSelector::Find for selector {0} {1}", i, s.ToString()));
        //        var elements = new List<NMElement>();
        //        elements.AddRange(current);
        //        current.Clear();
        //        foreach (var _element in elements)
        //        {
        //            result = ((NMSelectorItem)s).matches(_element);
        //            current.AddRange(result);
        //        }
        //        if (i == (selectors.Count - 1)) result = current.ToArray();
        //        if (current.Count == 0)
        //        {
        //            var _c = new NMSelectorItem(selectors[i]);
        //            var message = "needed to find " + Environment.NewLine + _c.ToString() + Environment.NewLine + "but found only: " + Environment.NewLine;
        //            foreach (var element in elements)
        //            {
        //                var children = element.Children;
        //                foreach (var c in children)
        //                {
        //                    try
        //                    {
        //                        message += c.ToString() + Environment.NewLine;
        //                    }
        //                    catch (Exception)
        //                    {
        //                    }
        //                }
        //            }
        //            Log.Selector(message);
        //            return new NMElement[] { };
        //        }
        //        Log.Selector(string.Format("OpenRPA.NM::GetElement::found {1} for selector {2} in {0:mm\\:ss\\.fff}", sw.Elapsed, elements.Count(), i));
        //    }
        //    if (result == null) return new NMElement[] { };
        //    return result;
        //}
        public static NMElement[] GetElementsWithuiSelector(NMSelector selector, IElement fromElement = null, int maxresults = 1)
        {
            var                  results = new List <NMElement>();
            SelectorItem         first   = null;
            SelectorItem         second  = null;
            string               browser = "";
            SelectorItemProperty p       = null;

            if (selector.Count > 1)
            {
                first  = selector[0];
                second = selector[1];
                p      = first.Properties.Where(x => x.Name == "browser").FirstOrDefault();
                if (p != null)
                {
                    browser = p.Value;
                }
            }
            else if (fromElement == null)
            {
                throw new ArgumentException("Invalid select with onlu 1 child and no anchor");
            }
            else
            {
                second = selector[0];
            }
            p = second.Properties.Where(x => x.Name == "xpath").FirstOrDefault();
            string xpath = "";

            if (p != null)
            {
                xpath = p.Value;
            }
            p = second.Properties.Where(x => x.Name == "cssselector").FirstOrDefault();
            string cssselector = "";

            if (p != null)
            {
                cssselector = p.Value;
            }
            NMElement fromNMElement = fromElement as NMElement;

            if (fromElement != null)
            {
                browser = fromNMElement.browser;
                //if (browser != fromNMElement.browser) throw new ArgumentException("browser mismatch " + browser + "/" + fromNMElement.browser);
                p = second.Properties.Where(x => x.Name == "cssselector").FirstOrDefault();
                if (p == null)
                {
                    throw new ArgumentException("fromElement missing cssselector");
                }
                // xpath += p.Value.Substring(1);
                xpath       = "";
                cssselector = fromNMElement.cssselector + " > " + p.Value;
            }

            NMHook.checkForPipes(true, true);
            NMHook.reloadtabs();
            var tabs = NMHook.tabs;

            if (!string.IsNullOrEmpty(browser))
            {
                tabs = NMHook.tabs.Where(x => x.browser == browser).ToList();
            }
            foreach (var tab in tabs)
            {
                NativeMessagingMessage subresult = null;

                var getelement = new NativeMessagingMessage("getelements");
                getelement.browser = browser;
                getelement.xPath   = xpath;
                getelement.cssPath = cssselector;
                subresult          = NMHook.sendMessageResult(getelement, false, TimeSpan.FromSeconds(2));
                if (subresult != null)
                {
                    if (subresult.results != null)
                    {
                        foreach (var b in subresult.results)
                        {
                            if (b.cssPath == "true" || b.xPath == "true")
                            {
                                var data = b.result;
                                var arr  = JArray.Parse(data);
                                foreach (var _e in arr)
                                {
                                    if (results.Count > maxresults)
                                    {
                                        continue;
                                    }
                                    var json         = _e.ToString();
                                    var subsubresult = Newtonsoft.Json.JsonConvert.DeserializeObject <NativeMessagingMessage>(json);
                                    subsubresult.browser = browser;
                                    subsubresult.result  = json;
                                    subsubresult.tabid   = b.tabid;
                                    subsubresult.tab     = b.tab;
                                    results.Add(new NMElement(subsubresult));
                                }
                            }
                        }
                    }
                }
            }
            return(results.ToArray());
            //NMHook.Instance.refreshJvms();
            //NMElement[] result = null;
            //foreach (var jvm in NMHook.Instance.jvms)
            //{
            //    result = GetElementsWithuiSelector(jvm, selector, fromElement, maxresults);
            //    if (result.Count() > 0) return result;
            //}

            //if (result == null) return new NMElement[] { };
            //return result;
            //return new NMElement[] { };
        }
Exemple #5
0
        public static NMElement[] GetElementsWithuiSelector(NMSelector selector, IElement fromElement = null, int maxresults = 1)
        {
            var                  results = new List <NMElement>();
            SelectorItem         first   = null;
            SelectorItem         second  = null;
            string               browser = "";
            SelectorItemProperty p       = null;

            if (selector.Count > 1)
            {
                first  = selector[0];
                second = selector[1];
                p      = first.Properties.Where(x => x.Name == "browser").FirstOrDefault();
                if (p != null)
                {
                    browser = p.Value;
                }
            }
            else if (fromElement == null)
            {
                throw new ArgumentException("Invalid select with only 1 child and no anchor");
            }
            else
            {
                second = selector[0];
            }
            p = second.Properties.Where(x => x.Name == "xpath").FirstOrDefault();
            string xpath = "";

            if (p != null)
            {
                xpath = p.Value;
            }
            p = second.Properties.Where(x => x.Name == "cssselector").FirstOrDefault();
            string cssselector = "";

            if (p != null)
            {
                cssselector = p.Value;
            }
            NMElement fromNMElement = fromElement as NMElement;
            string    fromcssPath   = "";
            string    fromxPath     = "";

            if (fromElement != null)
            {
                fromcssPath = fromNMElement.cssselector;
                fromxPath   = fromNMElement.xpath;
            }
            //NMHook.checkForPipes(true, true);
            //NMHook.reloadtabs();
            //var tabs = NMHook.tabs.ToList();
            //if (!string.IsNullOrEmpty(browser)) {
            //    lock(NMHook.tabs)
            //    {
            //        tabs = NMHook.tabs.Where(x => x.browser == browser).ToList();
            //    }
            //}
            //foreach (var tab in tabs)
            //{

            //}
            NativeMessagingMessage subresult = null;

            var getelement = new NativeMessagingMessage("getelements", PluginConfig.debug_console_output);

            getelement.browser     = browser;
            getelement.xPath       = xpath;
            getelement.cssPath     = cssselector;
            getelement.fromxPath   = fromxPath;
            getelement.fromcssPath = fromcssPath;
            if (fromElement != null && fromElement is NMElement)
            {
                getelement.windowId = ((NMElement)fromElement).message.windowId;
                getelement.tabid    = ((NMElement)fromElement).message.tabid;
                getelement.frameId  = ((NMElement)fromElement).message.frameId;
            }
            subresult = NMHook.sendMessageResult(getelement, false, TimeSpan.FromSeconds(2));
            if (subresult != null)
            {
                if (subresult.results != null)
                {
                    foreach (var b in subresult.results)
                    {
                        if (b.cssPath == "true" || b.xPath == "true")
                        {
                            if (results.Count > maxresults)
                            {
                                continue;
                            }
                            results.Add(new NMElement(b));
                            //var data = b.result;
                            //var arr = JArray.Parse(data.ToString());
                            //foreach (var _e in arr)
                            //{
                            //    if (results.Count > maxresults) continue;
                            //    var json = _e.ToString();
                            //    var subsubresult = Newtonsoft.Json.JsonConvert.DeserializeObject<NativeMessagingMessage>(json);
                            //    subsubresult.browser = browser;
                            //    subsubresult.result = json;
                            //    subsubresult.tabid = b.tabid;
                            //    subsubresult.tab = b.tab;
                            //    results.Add(new NMElement(subsubresult));
                            //}
                        }
                    }
                }
            }
            return(results.ToArray());
        }
Exemple #6
0
        //private static NMElement[] GetElementsWithuiSelector(WindowsAccessBridgeInterop.AccessibleJvm jvm, NMSelector selector, IElement fromElement, int maxresults)
        //{
        //    NMElement[] result = null;
        //    NMElement _fromElement = fromElement as NMElement;
        //    var selectors = selector.Where(x => x.Enabled == true && x.Selector == null).ToList();
        //    var current = new List<NMElement>();
        //    NMElement startfrom = null;
        //    if (_fromElement != null) startfrom = _fromElement;
        //    if (startfrom == null) startfrom = new NMElement(jvm);
        //    current.Add(startfrom);
        //    for (var i = 0; i < selectors.Count; i++)
        //    {
        //        var sw = new System.Diagnostics.Stopwatch();
        //        sw.Start();
        //        var s = new NMSelectorItem(selectors[i]);
        //        Log.Selector(string.Format("OpenRPA.NM::GetElementsWithuiSelector::Find for selector {0} {1}", i, s.ToString()));
        //        var elements = new List<NMElement>();
        //        elements.AddRange(current);
        //        current.Clear();
        //        foreach (var _element in elements)
        //        {
        //            result = ((NMSelectorItem)s).matches(_element);
        //            current.AddRange(result);
        //        }
        //        if (i == (selectors.Count - 1)) result = current.ToArray();
        //        if (current.Count == 0)
        //        {
        //            var _c = new NMSelectorItem(selectors[i]);
        //            var message = "needed to find " + Environment.NewLine + _c.ToString() + Environment.NewLine + "but found only: " + Environment.NewLine;
        //            foreach (var element in elements)
        //            {
        //                var children = element.Children;
        //                foreach (var c in children)
        //                {
        //                    try
        //                    {
        //                        message += c.ToString() + Environment.NewLine;
        //                    }
        //                    catch (Exception)
        //                    {
        //                    }
        //                }
        //            }
        //            Log.Selector(message);
        //            return new NMElement[] { };
        //        }
        //        Log.Selector(string.Format("OpenRPA.NM::GetElement::found {1} for selector {2} in {0:mm\\:ss\\.fff}", sw.Elapsed, elements.Count(), i));
        //    }
        //    if (result == null) return new NMElement[] { };
        //    return result;
        //}
        public static NMElement[] GetElementsWithuiSelector(NMSelector selector, IElement fromElement = null, int maxresults = 1)
        {
            var    results = new List <NMElement>();
            var    first   = selector[0];
            var    second  = selector[1];
            var    p       = first.Properties.Where(x => x.Name == "browser").FirstOrDefault();
            string browser = "";

            if (p != null)
            {
                browser = p.Value;
            }
            p = second.Properties.Where(x => x.Name == "xpath").FirstOrDefault();
            string xpath = "";

            if (p != null)
            {
                xpath = p.Value;
            }
            p = second.Properties.Where(x => x.Name == "cssselector").FirstOrDefault();
            string cssselector = "";

            if (p != null)
            {
                cssselector = p.Value;
            }

            NMHook.checkForPipes(true, true);
            NMHook.reloadtabs();
            var tabs = NMHook.tabs;

            if (!string.IsNullOrEmpty(browser))
            {
                tabs = NMHook.tabs.Where(x => x.browser == browser).ToList();
            }
            foreach (var tab in tabs)
            {
                NativeMessagingMessage subresult = null;

                var getelement = new NativeMessagingMessage("getelements");
                getelement.browser = browser;
                getelement.xPath   = xpath;
                getelement.cssPath = cssselector;
                subresult          = NMHook.sendMessageResult(getelement, false, TimeSpan.FromSeconds(2));
                if (subresult != null)
                {
                    foreach (var b in subresult.results)
                    {
                        if (b.cssPath == "true" || b.xPath == "true")
                        {
                            var data = b.result;
                            var arr  = JArray.Parse(data);
                            foreach (var _e in arr)
                            {
                                var json         = _e.ToString();
                                var subsubresult = Newtonsoft.Json.JsonConvert.DeserializeObject <NativeMessagingMessage>(json);
                                subsubresult.browser = browser;
                                subsubresult.result  = json;
                                subsubresult.tabid   = b.tabid;
                                subsubresult.tab     = b.tab;
                                results.Add(new NMElement(subsubresult));
                            }
                        }
                    }
                }
            }
            return(results.ToArray());
            //NMHook.Instance.refreshJvms();
            //NMElement[] result = null;
            //foreach (var jvm in NMHook.Instance.jvms)
            //{
            //    result = GetElementsWithuiSelector(jvm, selector, fromElement, maxresults);
            //    if (result.Count() > 0) return result;
            //}

            //if (result == null) return new NMElement[] { };
            //return result;
            //return new NMElement[] { };
        }
Exemple #7
0
        public static NMElement[] GetElementsWithuiSelector(NMSelector selector, IElement fromElement = null, int maxresults = 1)
        {
            var                  results = new List <NMElement>();
            SelectorItem         first   = null;
            SelectorItem         second  = null;
            string               browser = "";
            SelectorItemProperty p       = null;

            if (selector.Count > 1)
            {
                first  = selector[0];
                second = selector[1];
                p      = first.Properties.Where(x => x.Name == "browser").FirstOrDefault();
                if (p != null)
                {
                    browser = p.Value;
                }
            }
            else if (fromElement == null)
            {
                throw new ArgumentException("Invalid select with only 1 child and no anchor");
            }
            else
            {
                second = selector[0];
            }
            p = second.Properties.Where(x => x.Name == "xpath").FirstOrDefault();
            string xpath = "";

            if (p != null)
            {
                xpath = p.Value;
            }
            p = second.Properties.Where(x => x.Name == "cssselector").FirstOrDefault();
            string cssselector = "";

            if (p != null)
            {
                cssselector = p.Value;
            }
            NMElement fromNMElement = fromElement as NMElement;
            string    fromcssPath   = "";
            string    fromxPath     = "";

            if (fromElement != null)
            {
                fromcssPath = fromNMElement.cssselector;
                fromxPath   = fromNMElement.xpath;
            }
            NativeMessagingMessage subresult = null;

            var getelement = new NativeMessagingMessage("getelements", PluginConfig.debug_console_output, PluginConfig.unique_xpath_ids);

            getelement.browser     = browser;
            getelement.xPath       = xpath;
            getelement.cssPath     = cssselector;
            getelement.fromxPath   = fromxPath;
            getelement.fromcssPath = fromcssPath;
            if (fromElement != null && fromElement is NMElement)
            {
                getelement.windowId = ((NMElement)fromElement).message.windowId;
                getelement.tabid    = ((NMElement)fromElement).message.tabid;
                getelement.frameId  = ((NMElement)fromElement).message.frameId;
            }
            subresult = NMHook.sendMessageResult(getelement, PluginConfig.protocol_timeout);
            if (subresult != null)
            {
                if (subresult.results != null)
                {
                    foreach (var b in subresult.results)
                    {
                        if (b.cssPath == "true" || b.xPath == "true")
                        {
                            if (results.Count > maxresults)
                            {
                                continue;
                            }
                            var nme = new NMElement(b);
                            results.Add(nme);
                        }
                    }
                }
            }
            return(results.ToArray());
        }