javascriptgoogle-chromedomxpathdocument.evaluate

document.evaluate not working in Chrome?


I've got the following..

 var result = doc.evaluate("//input[@class=\"form_field_as as-input\"]", 
                           context, 
                           null, 
                           XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, 
                           null);
    for(var i = 0; i < result.snapshotLength; i++) {
    a[i] = result.snapshotItem(i);
}
return a;

The expression I'm evaluating is from an input. The code works fine in FireFox but when I test it on Chrome it doesn't return anything. What am I doing wrong?

The input I'm evaluating is..

<input type="text" id="sharees" class="form_field_as">

Solution

  • From http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathEvaluator-evaluate

    contextNode of type Node
    The context is context node for the evaluation of this XPath expression. If the XPathEvaluator was obtained by casting the Document then this must be owned by the same document and must be a Document, Element, Attribute, Text, CDATASection, Comment, ProcessingInstruction, or XPathNamespace node. If the context node is a Text or a CDATASection, then the context is interpreted as the whole logical text node as seen by XPath, unless the node is empty in which case it may not serve as the XPath context.

    So, your context must be some instance of these classes. I guess that you probably don't set this variable at all. You could also use null and the context would become the node from wich you are evaluating the expression.

    Besides that, do note that //input[@class='form_field_as as-input'] is an absolute expression and it will return the same result from any context (outside document context is not allowed when "XPathEvaluator was obtained by casting the Document" ).