in reply to Some issues with WWW::Mechanize::Firefox->xpath() method

I would assume that the substring() function wants a string and not a node, and thus one would need to use //p/text() to get at the node text if that is what's wanted.

The code of WWW::Mechanize::Firefox basically passes XPath queries straight through to Firefox, so if there is a Javascript error raised by the XPath method, that error most likely comes directly from Firefox itself.

Replies are listed 'Best First'.
Re^2: Some issues with WWW::Mechanize::Firefox->xpath() method
by Anonymous Monk on Apr 02, 2013 at 11:12 UTC
    FWIW, I tried with LibXML but I can't get no results with these queries either
    print for $dom->findnodes(q{ substring( //p/text() , 0, 4 ) }); print for $dom->findnodes(q{ substring(string(//title),1,4) }); print for $dom->findnodes(q{ substring(string(//title/text()),1,4) });
Re^2: Some issues with WWW::Mechanize::Firefox->xpath() method
by dfaure (Chaplain) on Apr 02, 2013 at 12:23 UTC
    The code of WWW::Mechanize::Firefox basically passes XPath queries straight through to Firefox, so if there is a Javascript error raised by the XPath method, that error most likely comes directly from Firefox itself.

    Could'nt the issue come from the js glue between Perl and Firefox, being unable to deal with XPathResults different from nodes?

    ____
    HTH, Dominique
    My two favorites:
    If the only tool you have is a hammer, you will see every problem as a nail. --Abraham Maslow
    Bien faire, et le faire savoir...

      The JS glue just passes along whatever document.evaluate returns, after converting that array-like XPathResults list into a plain array:

      function(doc,q,ref,cont) { var xres = doc.evaluate(q,ref,null,XPathResult.ORDERED_NODE_SN +APSHOT_TYPE, null ); var map; if( cont ) { map = cont; } else { // Default is identity map = function(e){ return e }; }; var res = []; for ( var i=0 ; i < xres.snapshotLength; i++ ) { res.push( map(xres.snapshotItem(i))); }; return res }

      I'm no expert on XPath and its semantics, but if somebody submits a bug report and preferrably a self-contained example, I can investigate things closer.

        I'm no expert on XPath and its semantics, but if somebody submits a bug report and preferrably a self-contained example, I can investigate things closer.

        You may safely use the code I provided in the OP as example. I'm simply using Firebug+FirePath addons on the target Firefox, to get results from javascript only.

        According to https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIDOMXPathResult#Type_constants, I think the problem comes from the parameters given to the XPath evaluator, which limits the retrieved elements to nodesets. IMHO, the solution would be to use XPathResult.ANY_TYPE and perhaps a more elaborated function than identity to build the result set.

        Should I open a bug on RT for this?

        ____
        HTH, Dominique
        My two favorites:
        If the only tool you have is a hammer, you will see every problem as a nail. --Abraham Maslow
        Bien faire, et le faire savoir...

      I think its your code, instead of Dumpering %results dump @xpr and look for stringValue