2012-02-10 35 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