簡體   English   中英

如何處理具有自定義html屬性的多選列表?

[英]how to deal with a multiple select list with custom html attributes?

<select class="business_group" multiple="multiple" name="SelectedBusinessGroups">
  <option value="Partners">Partners</option>
  <option value="Press">Press</option>
  <option value="ProductToolbox">Product Toolbox</option>
  <option selected="selected" value="Promotional">Promotional</option>
  <option value="Sponsors">Sponsors</option>
</select>

名為:selected的屬性表示單擊該選項。 我想檢查選項列表中是否選擇了“促銷”。 我怎樣才能做到這一點?

我試過了

assert @browser.option(:text => "Promotional").attribute_value("selected").exists? == true

但它不起作用。

您有幾個選項可以檢查所選選項。

使用選項#?

選項有一個內置的方法來告訴你它們是否被選中 - 參見Option#selected?

@browser.option(:text => "Promotional").selected?
#=> true

@browser.option(:text => "Press").selected?
#=> false

使用Select#selected?

選擇列表具有內置方法,用於檢查是否選擇了選項 - Select#selected? 請注意,這僅根據文本檢查選項。

ddl = @browser.select(:class => 'business_group')

ddl.selected?('Promotional')
#=> true

ddl.selected?('Press')
#=> false

使用Select#selected_options

Select#selected_options方法將返回所選選項的集合。 您遍歷此集合以查看是否包含所需的選項。 這允許您通過多於其文本檢查選項。

selected = @browser.select(:class => 'business_group').selected_options
selected.any?{ |o| o.text == 'Promotional' }
#=> true

使用Element#attribute_value

如果屬性存在, attribute_value方法將返回屬性值作為字符串。 如果該屬性不存在,則返回nil。

#Compare the attribute value
@browser.option(:text => "Promotional").attribute_value("selected") == 'true'
#=> true

#Compare the attribute value presence
@browser.option(:text => "Promotional").attribute_value("selected").nil? == false
#=> true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM