appleLover
appleLover

Reputation: 15691

Ruby - Find Tag by ID

I'm using mechanize and nokogiri. I'm trying to find this tag. When I inspect the HTML it looks like this.

<table class="matchupBox" id="MLB_5_block ">

When I print it out in my console it looks like this

#<Nokogiri::XML::Element:0x2cc1a1c name="table" attributes=[
#<Nokogiri::XML::Attr:0x2cc1940 name="class" value="matchupBox">,
 #<Nokogiri::XML::Attr:0x2cc192c name="id" value="MLB_5_block\r\n            ">]

I am using this code.

doc.search("table#MLB_5_block")

but it doesn't match. Whereas this does match

doc.search("table.matchupBox")

so I think the problem has to do with that "\r\n " white space the site is adding. How do I match it, using the id, and without being dependent on that whitespace?

Upvotes: 0

Views: 114

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118261

You try to do :

doc.search("//table[normalize-space(@id) = 'MLB_5_block']")

The normalize-space function returns the argument string with whitespace normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters by a single space.

Upvotes: 1

Related Questions