如何检查watir中的文本颜色以获取下拉框属性

时间:2012-02-10 12:21:44

标签: ruby automation watir

contents = $ie.select_list(:id, "dropdown").getAllContents
puts contents.currentstyle.color

显示错误为

1) Error:
test_01(TC_Login):
NoMethodError: undefined method `currentstyle' for

任何人都可以提供帮助,因为我需要获取特定颜色的特定记录

1 个答案:

答案 0 :(得分:1)

假设你有这样的HTML:

<select name="list" id="select_list">
  <option value="1" style="color:blue" SELECTED>Name1</option>
  <option value="2" style="color:green">Name2</option>
  <option value="3" style="color:green">Name3</option>
</select>

我找到获得选项颜色的唯一方法是直接访问它(来自win32ole对象)。以下将输出第一个选项的颜色。

puts $ie.select_list(:id, "dropdown").document.options(0).style.color

如果你想获得具有匹配颜色的Watir :: Option对象,你可以这样做:

matching_colour = 'green'   # Colour you want

# Iterate through the options to find the first match
select_list_element = ie.select_list(:id, 'dropdown')
matching_option = nil
select_list_element.document.options.each{ |o|
  if o.style.color == matching_colour
    matching_option = select_list_element.option(:text, o.text)
    break
  end
}

# Do something with the option if one was found
if match_option.nil?
  #Nothing matches
else
  #Do something with the option, like select it
  matching_option.select
end