示例#1
0
        public bool Match(SelectorItem item, IElement m)
        {
            var p = item.Properties.Where(x => x.Name == "xpath").FirstOrDefault();

            //if(p != null)
            //{
            //    var xpath = p.Value;
            //    var browser = Browser.GetBrowser();
            //    if(browser != null)
            //    {
            //        MSHTML.IHTMLDocument3 sourceDoc = (MSHTML.IHTMLDocument3)browser.Document;
            //        string documentContents = sourceDoc.documentElement.outerHTML;
            //        HtmlAgilityPack.HtmlDocument targetDoc = new HtmlAgilityPack.HtmlDocument();
            //        targetDoc.LoadHtml(documentContents);
            //        HtmlAgilityPack.HtmlNode node = targetDoc.DocumentNode.SelectSingleNode(xpath);
            //        if(node != null)
            //        {
            //            var ele = new IEElement(browser, node);

            //        }
            //    }
            //}
            return(IESelectorItem.Match(item, m.RawElement as MSHTML.IHTMLElement));
        }
示例#2
0
文件: Plugin.cs 项目: zergmk2/openrpa
 public bool Match(SelectorItem item, IElement m)
 {
     return(IESelectorItem.Match(item, m.RawElement as MSHTML.IHTMLElement));
 }
示例#3
0
        private void enumElements(Browser browser, MSHTML.IHTMLElement baseelement, IESelector anchor, bool doEnum, int X, int Y)
        {
            MSHTML.IHTMLElement element  = baseelement;
            MSHTML.HTMLDocument document = browser.Document;
            var pathToRoot = new List <MSHTML.IHTMLElement>();

            while (element != null)
            {
                if (pathToRoot.Contains(element))
                {
                    break;
                }
                try
                {
                    pathToRoot.Add(element);
                }
                catch (Exception)
                {
                }
                try
                {
                    element = element.parentElement;
                }
                catch (Exception ex)
                {
                    Log.Error(ex.ToString());
                    return;
                }
            }
            // Log.Selector(string.Format("IEselector::create pathToRoot::end {0:mm\\:ss\\.fff}", sw.Elapsed));
            pathToRoot.Reverse();
            if (anchor != null)
            {
                // var hasIframe = anchor.Where(x => x.Properties.Where(y=>y.Name=="tagName" && y.Value== "IFRAME").Count()>0).Count() > 0;
                var iframeidx = 0;
                for (var i = 0; i < anchor.Count(); i++)
                {
                    if (anchor[i].Properties.Where(y => y.Name == "tagName" && y.Value == "IFRAME").Count() > 0)
                    {
                        iframeidx = i;
                        break;
                    }
                }
                var anchorlist = anchor.Where(x => x.Enabled && x.Selector == null).ToList();
                //if (iframeidx>-1)
                //{
                //    for (var i = 0; i < iframeidx; i++)
                //    {
                //        pathToRoot.Remove(pathToRoot[0]);
                //    }
                //}
                for (var i = (iframeidx); i < anchorlist.Count(); i++)
                {
                    //if (((IESelectorItem)anchorlist[i]).Match(pathToRoot[0]))
                    if (IESelectorItem.Match(anchorlist[i], pathToRoot[0]))
                    {
                        pathToRoot.Remove(pathToRoot[0]);
                    }
                    else
                    {
                        Log.Selector("Element does not match the anchor path");
                        return;
                    }
                }
            }

            if (pathToRoot.Count == 0)
            {
                Log.Error("Element is same as annchor");
                return;
            }
            element = pathToRoot.Last();
            //
            // Log.Selector(string.Format("IEselector::remove anchor if needed::end {0:mm\\:ss\\.fff}", sw.Elapsed));
            IESelectorItem item;

            if (anchor == null && Items.Count == 0)
            {
                item         = new IESelectorItem(browser.Document);
                item.Enabled = true;
                //item.canDisable = false;
                Items.Add(item);
            }
            for (var i = 0; i < pathToRoot.Count(); i++)
            {
                var o = pathToRoot[i];
                item = new IESelectorItem(browser, o);
                if (i == 0 || i == (pathToRoot.Count() - 1))
                {
                    item.canDisable = false;
                }
                foreach (var p in item.Properties)
                {
                    int idx = p.Value.IndexOf(".");
                    if (p.Name == "className" && idx > -1)
                    {
                        int idx2 = p.Value.IndexOf(".", idx + 1);
                        if (idx2 > idx)
                        {
                            p.Value = p.Value.Substring(0, idx2 + 1) + "*";
                        }
                    }
                }
                if (doEnum)
                {
                    item.EnumNeededProperties(o, o.parentElement);
                }

                Items.Add(item);
            }
            if (frameTags.Contains(baseelement.tagName.ToUpper()))
            {
                //var ele2 = baseelement as MSHTML.IHTMLElement2;
                //var col2 = ele2.getClientRects();
                //var rect2 = col2.item(0);
                //X -= rect2.left;
                //Y -= rect2.top;
                var frame = baseelement as MSHTML.HTMLFrameElement;
                var fffff = frame.contentWindow;
                MSHTML.IHTMLWindow2 window = frame.contentWindow;
                MSHTML.IHTMLElement el2    = null;
                foreach (string frameTag in frameTags)
                {
                    MSHTML.IHTMLElementCollection framesCollection = document.getElementsByTagName(frameTag);
                    foreach (MSHTML.IHTMLElement _frame in framesCollection)
                    {
                        // var _f = _frame as MSHTML.HTMLFrameElement;
                        el2 = browser.ElementFromPoint(_frame, X, Y);
                        //var _wb = _f as SHDocVw.IWebBrowser2;
                        //document = _wb.Document as MSHTML.HTMLDocument;
                        //el2 = document.elementFromPoint(X, Y);
                        if (el2 != null)
                        {
                            var tag = el2.tagName;
                            // var html = el2.innerHTML;
                            Log.Selector("tag: " + tag);

                            //browser.elementx += _frame.offsetLeft;
                            //browser.elementy += _frame.offsetTop;
                            //browser.frameoffsetx += _frame.offsetLeft;
                            //browser.frameoffsety += _frame.offsetTop;


                            enumElements(browser, el2, anchor, doEnum, X, Y);
                            return;
                        }
                    }
                }
            }
        }