开发者

Watir - How to get option's attribute (aside from key and value)

I'm trying to inspect a select_list element with options that have a custom attribute. Like in the following example, each option has a "title"

<select id="mySelectList">
  <option title="title1" value="1">Title 1</option>
  <option title="title2" value="2">Title 2</option>
  <option title="title3" value开发者_JS百科="3">Title 3</option>
</select>

I need access to the options' title, but each option doesn't really respond to the attribute_value method. Is it possible to retrieve values of a custom attribute for options in Watir?

I'm using watir and IE


For the HTML you have provided, this worked for me, using watir-webdriver and Firefox:

browser.option(:text => "Title 3").attribute_value "title"
=> "title3" 

If you need all of them:

browser.options.each {|option| puts option.attribute_value "title" }

outputs

title1
title2
title3


Using watir gem (2.0.2) and Internet Explorer 9:

browser.element(:css => "option").attribute_value "title"
=> "title1"

or

browser.select_list(:id => "mySelectList").elements.each {|option| puts option.attribute_value "title" }

outputs

title1
title2
title3
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜