Forum: Ruby nokogiri xpath method

Posted by gaurav shirodkar (gau)
on 2013-02-03 08:41
Hi I am having a problem in nokogiri::HTML::Document.xpath method
for the site
http://services.ptcmysore.gov.in/RNetTracking/Trac...
Code
doc=
agent.get("http://services.ptcmysore.gov.in/RNetTracking/Trac...)

page1 = Nokogiri::HTML(open(doc.uri.to_s))

x = [page1.css("#GridView1 th")[0].text,page1.css("#GridView1
td")[0].text,page1.css("#GridView1 th")[1].text,page1.css("#GridView1
td")[1].text,page1.css("#GridView1 th")[2].text,page1.css("#GridView1
td")[2].text,page1.css("#GridView1 th")[3].text,page1.css("#GridView1
td")[3].text]
puts x
puts page1.css('#pnlDetail div #GridView2 tr th')[0].text#Error

india.rb:22:in `<main>': undefined method `text' for nil:NilClass
(NoMethodError
)
Posted by Love U Ruby (my-ruby)
on 2013-02-03 09:14
adding myself to this trail from personal interest.
Posted by Carlo E. Prelz (Guest)
on 2013-02-03 09:48
(Received via mailing list)
Subject: nokogiri xpath method
  Date: dom 03 feb 13 04:41:53 +0900

Quoting gaurav shirodkar (lists@ruby-forum.com):

> india.rb:22:in `<main>': undefined method `text' for nil:NilClass
> (NoMethodError
> )

What's your problem there? Check line 22 of your source code. It has
to be that one of the CSS tags you ask for does not exist in your
page. Instead of immediately asking for, let's say

> page1.css("#GridView1 th")[0].text

you should first check if

page1.css("#GridView1 th")

is not null. Or maybe catch the exception? You should refactor...

Carlo
Posted by Robert Klemme (robert_k78)
on 2013-02-04 20:49
(Received via mailing list)
On Sun, Feb 3, 2013 at 9:48 AM, Carlo E. Prelz <fluido@fluido.as> wrote:

> You should refactor...

Definitively: I can see serious violation of DRY. :-)

Cheers

robert
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.