Access HTML attributes in Nokigiri

Here is the code I'm using:

location = block.xpath("*/img")
puts location

And it gives out:

<img src="/images/p.gif" height="1" width="0">

What I want to do is get the width attribute from html, but I can't get it to work. I think I need to post ['width']somewhere in my code, and I tried several examples on the Internet, but couldn't get it to work.

+5
source share
2 answers

Take a look at the xpath syntax from this XPath Tutorial .

Try block.at_xpath("*/img")["width"]or */img/@widthif there is only one item.

+6
source

CSS selectors tend to be lighter and easier to read:

puts block.at('img')[:height]
+12
source

All Articles