4

Can I execute a XPath query on previous result? I have this xpath:

my @objDivRes = $objBrow->xpath('//div[@id="result"]/ol/div/li', all => 1);

but when I execute xpath function on previous result

my @objLink = $objDivRes[0]->MozRepl::RemoteObject::Methods::xpath('//div/h3/a');

I got an error:

MozRepl::RemoteObject: TypeError: doc.evaluate is not a function at test.pl

Is there an example? Thank you

  • 2
    @ThisSuitIsBlackNot, The syntax is valid, though it's rarely used outside of `$self->SUPER::method()`. It does look it's [expected](http://search.cpan.org/perldoc?MozRepl::RemoteObject::Methods) in this situation. (Basically, it's to avoid injecting `xpath` is a number of other classes.) – ikegami Mar 26 '14 at 14:48
  • @ikegami Well, I learned something today. – ThisSuitIsBlackNot Mar 26 '14 at 14:50
  • Have you tried forcing the return type of the `xpath` method as described in the [documentation](https://metacpan.org/pod/WWW::Mechanize::Firefox#mech-xpath-query-options), e.g. `my @objDivRes = $objBrow->xpath('//div[@id="result"]/ol/div/li', all => 1, type => $objBrow->xpathResult('ORDERED_NODE_SNAPSHOT_TYPE') );` – ThisSuitIsBlackNot Mar 28 '14 at 20:34
  • The [docs for MozRepl::RemoteObject::Methods](https://metacpan.org/pod/MozRepl::RemoteObject::Methods#obj-MozRepl::RemoteObject::Methods::xpath-query-ref-cont) say that `MozRepl::RemoteObject::Methods::xpath` should only be called on `HTMLdocument` nodes, so perhaps a type mismatch is causing your issue. – ThisSuitIsBlackNot Mar 28 '14 at 20:37

1 Answers1

1

Just use 'node' option to set up a subtree $mech->xpath( $query, %options )

Note dot in the beginning of the path, which means descendands of the context node

my @objDivRes = $objBrow->xpath('//div[@id="result"]/ol/div/li', all => 1);

my @objLink = $objBrow->xpath('.//div/h3/a', node => $objDivRes[0]);
Andrew Selivanov
  • 1,186
  • 14
  • 20