2012-02-10 64 views
1
contents = $ie.select_list(:id, "dropdown").getAllContents 
puts contents.currentstyle.color 

它显示为如何检查文本颜色的Watir的下拉框属性

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

任何一个可以帮助我需要特定的颜色

+0

这将是更好看HTML和CSS。也许风格不是为下拉内容设置的,而是下拉本身。无论如何,这对于人们帮助你的信息很重要。 – 2012-02-10 15:00:10

回答

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