private static string getNode(MSHTML.IHTMLElement node) { string nodeExpr = node.tagName; if (nodeExpr == null) // Eg. node = #text { return(null); } if (node.id != "" && node.id != null) { nodeExpr += "[@id='" + node.id + "']"; // We don't really need to go back up to //HTML, since IDs are supposed // to be unique, so they are a good starting point. return("/" + nodeExpr); } // Find rank of node among its type in the parent int rank = 1; MSHTML.IHTMLDOMNode nodeDom = node as MSHTML.IHTMLDOMNode; MSHTML.IHTMLDOMNode psDom = nodeDom.previousSibling; MSHTML.IHTMLElement ps = psDom as MSHTML.IHTMLElement; while (ps != null) { if (ps.tagName == node.tagName) { rank++; } psDom = psDom.previousSibling; ps = psDom as MSHTML.IHTMLElement; } if (rank > 1) { nodeExpr += "[" + rank + "]"; } else { // First node of its kind at this level. Are there any others? MSHTML.IHTMLDOMNode nsDom = nodeDom.nextSibling; MSHTML.IHTMLElement ns = nsDom as MSHTML.IHTMLElement; while (ns != null) { if (ns.tagName == node.tagName) { // Yes, mark it as being the first one nodeExpr += "[1]"; break; } nsDom = nsDom.nextSibling; ns = nsDom as MSHTML.IHTMLElement; } } return(nodeExpr); }
private static string getNode(MSHTML.IHTMLElement node) { string nodeExpr = node.tagName.ToLower(); if (nodeExpr == null) // Eg. node = #text { return(null); } var ngreflectname = node.getAttribute("ng-reflect-name") as string; if (!string.IsNullOrEmpty(ngreflectname) && nodeExpr == "input") { nodeExpr += "[@ng-reflect-name='" + ngreflectname + "']"; return("/" + nodeExpr); } if (node.id != "" && node.id != null) { nodeExpr += "[@id='" + node.id + "']"; return("/" + nodeExpr); } var name = node.getAttribute("name") as string; if (!string.IsNullOrEmpty(name) && nodeExpr == "input") { nodeExpr += "[@name='" + name + "']"; return("/" + nodeExpr); } // Find rank of node among its type in the parent int rank = 1; MSHTML.IHTMLDOMNode nodeDom = node as MSHTML.IHTMLDOMNode; MSHTML.IHTMLDOMNode psDom = nodeDom.previousSibling; MSHTML.IHTMLElement ps = psDom as MSHTML.IHTMLElement; while (ps != null) { if (ps.tagName == node.tagName) { rank++; } psDom = psDom.previousSibling; ps = psDom as MSHTML.IHTMLElement; } if (rank > 1) { nodeExpr += "[" + rank + "]"; } else { // First node of its kind at this level. Are there any others? MSHTML.IHTMLDOMNode nsDom = nodeDom.nextSibling; MSHTML.IHTMLElement ns = nsDom as MSHTML.IHTMLElement; while (ns != null) { if (ns.tagName == node.tagName) { // Yes, mark it as being the first one nodeExpr += "[1]"; break; } nsDom = nsDom.nextSibling; ns = nsDom as MSHTML.IHTMLElement; } } return(nodeExpr); }