Find string on html page using mechanization

I am trying to find if a given line, say β€œHello”, exists on this page. So far I have the following:

agent = Mechanize.new page = agent.get('http://www.google.de/') 

and what should I do now? I saw the search method, but it only accepts XPath / CSS expressions. I could try using xpath to search, but is there a better way?

+4
source share
1 answer

You can simply do a general text search:

 page.body.include?('Hello') 

However, when searching in a specific html node, I recommend using the css selector as follows:

 page.parser.css('#my_container_element').text.include? 'Hello' 
+5
source

Source: https://habr.com/ru/post/1491613/


All Articles