String Contains Specified value - Yash-777/SeleniumDriverAutomation GitHub Wiki
DOM Tree as a String.
Trees are a widely-used type of data structure that emulates a tree design with a set of linked nodes.
DOM trees contain several kinds of nodes, in particular a DocumentType node, Element nodes, Text nodes, Comment nodes, and in some cases Processing Instruction nodes.
HTML pages include a document type declaration (sometimes referred to as !DOCTYPE statement). By specifying the document type declaration makes it easier to use a validator. We validate to check that all id attribute values are unique and that opening and closing tags are used according to the specification.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" >
...
</html>
- HTML String: The document element of this tree is the html element, which is the element always found in that position in HTML documents. It contains two elements, head and body, as well as a Text node between them.
var dom_HtmlString = document.documentElement.innerHTML;
- XMLSerializer to serialize DOM trees to strings
var nodeType = document;
var dom_XMLString = new XMLSerializer().serializeToString( nodeType );
Tree Structured string source to Document object using DOMParser
DOMParser to parse XML from strings into DOM trees
Syntax:
new DOMParser() : DOMParser
parseFromString(string : String, mimeType : String) : Document
mimeType must be one of: 'text/html', 'text/xml', 'application/xml', 'application/xhtml+xml', 'image/svg+xml'.
Example: We get an error when they are trying to parse content which involve having opening and closing tags that are not used according to specification.
function parseHtml2DOM(xmlStr) {
return ( new window.DOMParser() ).parseFromString(xmlStr, "text/html");
}
XPath « Element String « Element
Example on Test URL - https://www.opencart.com/index.php?route=account/login
OuterHTML - <input type="email" name="email" value="">
HTML DOM - <input type="email" name="email" value="">
XML DOM - <input type="email" name="email" value="" />
From XPath we get OuterHTML as ElementString, from then by using DOMParser we get an Element back.
var locator = '//table/tbody/tr[1]/td[2]/input';
var element = document.evaluate( locator, window.document, null, 9, null ).singleNodeValue;
var inputUser = element.outerHTML;
console.log('Outer HTML:', inputUser);
var val = dom_HtmlString.indexOf(inputUser); // <input type="email" name="email" value="">
if( val >= 0) {
console.log('True - HTML');
} else {
console.log('False - HTML');
}
var xml_to_DOM = parseHtml2DOM( dom_HtmlString ); //parseHtml2DOM( dom_XMLString );
var elementXML = document.evaluate( userPaht, xml_to_DOM/*nodeType*/, null, 9, null ).singleNodeValue;
console.log( elementXML );