I'm checking results of values to verify that they are correct.
我正在检查值的结果以验证它们是否正确。
Using watir-webdriver.
In this case javascript generates a color class:
在这种情况下,javascript会生成一个颜色类:
eg:
<span class="storyEdit limeGreen"> x </span>
in ruby currently I'm trying to parse the information from the using .html
目前我正在尝试解析使用.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:
我想只返回limeGreen所以我可以说:
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?
这将是几种不同的颜色,所以我想知道有没有办法只从HTML中提取类名?
If I haven't explained anything well enough, please feel free to let me know so I can add extra information!
如果我没有解释得足够好,请随时告诉我,以便我可以添加额外的信息!
2 个解决方案
#1
2
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.
Watir让你直接这样做;您不需要自己手动解析HTML。 Element#class_name方法将为您提供元素的类。
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.
从那里,你必须解析字符串以确定它是什么颜色。鉴于类可能是任何顺序和无限数量的可能颜色,我不相信有一般方法来获得颜色。解决方案取决于您想要对颜色做什么,以及是否提前知道可能的颜色。
#2
1
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
但是对于这类事情使用一些html解析库通常会更好,比如nokogiri或hpricot
#1
2
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.
Watir让你直接这样做;您不需要自己手动解析HTML。 Element#class_name方法将为您提供元素的类。
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.
从那里,你必须解析字符串以确定它是什么颜色。鉴于类可能是任何顺序和无限数量的可能颜色,我不相信有一般方法来获得颜色。解决方案取决于您想要对颜色做什么,以及是否提前知道可能的颜色。
#2
1
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
但是对于这类事情使用一些html解析库通常会更好,比如nokogiri或hpricot