Reputation: 779
I'm checking results of values to verify that they are correct.
Using watir-webdriver.
In this case javascript generates a color class:
eg:
<span class="storyEdit limeGreen"> x </span>
in ruby currently I'm trying to parse the information from the using .html
so this is something like what I've parse so far
=> <span class=\"storyEdit limeGreen\"> x </span>
I'd like to only return limeGreen so I can say:
color = resultOfParsedSpan
This would be for a few different colours, so I was wondering is there a way to only pull the class name from the html?
If I haven't explained anything well enough, please feel free to let me know so I can add extra information!
Upvotes: 1
Views: 179
Reputation: 46826
Watir let's you do this directly; you do not need to manually parse the HTML yourself. The Element#class_name
method will give you the element's class.
Example (assuming it is the first span):
browser.span.class_name
#=> "storyEdit limeGreen"
From that, you would have to parse the string to figure out what color it is. Given that the classes might be in any order and the infinite number of possible colors, I do not believe there is a general way to get just the color. The solution would depend on what you want to do with color
and if the possible colors are known ahead of time.
Upvotes: 2
Reputation: 5880
well, a quick approach would be something like this:
span = '<span class="storyEdit limeGreen"> x </span>'
color = $1.split.last if span =~ /class="(.*)"/
but it would be generally better to use some html parsing libraries for this sort of things, like nokogiri
or hpricot
Upvotes: 1