Search code examples
rubyselenium-webdrivermechanize-ruby

Is there anyway to search and get the value of <a>.. </a>?


In a webpage suppose i have the below values:

<td> <a href="https://www.test.com/test123/a.html"> test11 </a> </td>
<td> <a href="https://www.test.com/test12333/r.html"> test12 </a> </td>
<td> <a href="https://www.test.com/testaa123/t.html"> test21 </a> </td>
<td> <a href="https://www.test.com/test123123/b.html"> test31 </a> </td>

Is there anyway to find the value test21 using Ruby?

Or is there anyway to find the href values which has a substring /testaa123/t.html?


Solution

  • Try out this tutorial for Nokogiri.

    Example for a <li> tag:

    require 'rubygems'
    require 'nokogiri'   
    require 'open-uri'
    PAGE_URL = "http://ruby.bastardsbook.com/files/hello-webpage.html"
    
    page.css('li')[0].text
    

    This will output YouTube from the site below:

    <div id="funstuff">
       <p>Here are some entertaining links:</p>
       <ul>
          <li><a href="http://youtube.com">YouTube</a></li>
          <li><a data-category="news" href="http://reddit.com">Reddit</a></li>
          <li><a href="http://kathack.com/">Kathack</a></li>
          <li><a data-category="news" href="http://www.nytimes.com">New York Times</a></li>
      </ul>
    </div>