I am running Xpath 2.0 in my Rails project and tried to use the matches()
function that it provides like so:
page = Mechanize.new.get('http://www.example.com').parser
page.search("//a[matches(text(),'lagerfordon','i')]").first.text
which throws the following error:
RuntimeError: xmlXPathCompOpEval: function matches not found
What am I doing wrong?
Nokogiri (which Mechanize is built on) uses libxml which supports XPath 1.0, but not XPath 2.0, and so there is no matches
function available.
You could do a case sensitive match using the contains
function:
//a[contains(text(),'lagerfordon')]
In order to do a case insensitive match, or to use a more complex regular expression, you will need to filter the result from Mechanize in Ruby, or create a custom XPath function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With