how to get class of an element in ruby / watir?
Asked Answered
E

2

5

I've got code for a table header:

<thead>
<tr class="ui-jqgrid-labels ui-sortable" role="rowheader" style="">
    <th id="categories_formName" role="columnheader" class="ui-state-default ui-th-column ui-th-ltr" style="width: 250px;">
        <div id="jqgh_categories_formName" class="ui-jqgrid-sortable">Category Name</div>
    </th>
    <th id="categories_totalClicks" role="columnheader" class="ui-state-default ui-th-column ui-th-ltr" style="width: 99px;">
        <div id="jqgh_categories_totalClicks" class="ui-jqgrid-sortable">Clicks</div>
    </th>
    <th id="categories_avgCpc" role="columnheader" class="ui-state-default ui-th-column ui-th-ltr" style="width: 99px;">
        <div id="jqgh_categories_avgCpc" class="ui-jqgrid-sortable">Avg CPC($)</div>
    </th>
    <th id="categories_totalCost" role="columnheader" class="ui-state-default ui-th-column ui-th-ltr" style="width: 99px;">
        <div id="jqgh_categories_totalCost" class="ui-jqgrid-sortable">Total Cost($)</div>
    </th>
    <th id="categories_convertToSale" role="columnheader" class="ui-state-default ui-th-column ui-th-ltr disabledHeader" style="width: 99px;">
        <div id="jqgh_categories_convertToSale" class="ui-jqgrid-sortable">CTS(%)</div>
    </th>
    <th id="categories_totalOrders" role="columnheader" class="ui-state-default ui-th-column ui-th-ltr disabledHeader" style="width: 99px;">
        <div id="jqgh_categories_totalOrders" class="ui-jqgrid-sortable">Total Orders</div>
    </th>
    <th id="categories_totalSales" role="columnheader" class="ui-state-default ui-th-column ui-th-ltr disabledHeader" style="width: 99px;">
        <div id="jqgh_categories_totalSales" class="ui-jqgrid-sortable">Sales($)</div>
    </th>
    <th id="categories_costOfSale" role="columnheader" class="ui-state-default ui-th-column ui-th-ltr disabledHeader" style="width: 96px;">
        <div id="jqgh_categories_costOfSale" class="ui-jqgrid-sortable">COS(%)</div>
    </th>
</tr>

and need to find how many th tags has class "disabledHeader" or at least get class of a particular (addressed by id).

When I do:

cl = b.th(:xpath, '//th[@id="categories_convertToSale"]')
cl.exist?
=> true
cl.inspect
=> "#<Watir::TableHeaderCell:0x..f9b976cc1015b866a located=true selector={:xpath=>\"//th[@id=\\\"categories_convertToSale\\\"]\", :tag_name=>\"th\"}>"
cl.class
=> Watir::TableHeaderCell

cl[@class] or cl(:class) return errors.

b.element(:class, "disabledHeader").size returns method missing error.

How to address all the th-s of this class?

Eaton answered 16/4, 2013 at 17:53 Comment(3)
you can use nokogiri.Muggy
b.element(:class, "disabledHeader").size is returning a missing method error since you used element. size is only available for element collections, so you need to have elements (ie pluralized). Since you know they are th elements, you should be more specific as in Zeljko's answer.Fayina
@justin-ko: what could be the reason why b.ths(:class, "disabledHeader").size returns me 0?Eaton
P
7

This should do it:

browser.ths(:class => "disabledHeader").size
Psychedelic answered 16/4, 2013 at 18:12 Comment(2)
b.ths(:id, "categories_convertToSale").size ` => 1` b.ths(:class, "disabledHeader").size ` => 0`Eaton
Since the ID attribute is supposed to be unique within a page, we would normally expect any collection selected using ID to have a size of 1 unless you were using some kind of regular expression for the expected id value. Depending on what version of watir you are using, I think potentially for the second thing to work, you may need to use a regex there.. b.ths(:class, /disabledHeader/).sizePestle
P
10

Zeljko addressed your question about counting the number of th tags that match some pattern.

Regarding getting the class of something, that depends on which flavor of 'class' you are referring to.

Ruby Classes

Ruby is an object oriented language, objects being defined by the 'class' keyword in the language. Watir makes use of this, and has an internal object model that parallels HTML objects. In Ruby, the .class method returns the class of an object, this is hardwired into the language. (in fact you will not see the .class method described anywhere in the watir rDocs) This is what you were doing when you tried code that should have looked like this

b.th(:id => "categories_convertToSale").class

=> Watir::TableHeaderCell

It's telling you that the class of object returned by the .th method is a watir 'TableHeaderCell' (for more info, see the watir rdoc for the TableHeaderCell Object and/or the .th method )

HTML Class Attributes

The other flavor of 'class' is the HTML class attribute which is a standard attribute for nearly all element types in HTML. To get this you need to use watir's .attribute_value method along with the attribute you want to examine, to get attribute values of an element, or any object in watir like a TableHeaderCell that parallels HTML element types.

b.th(:id => "categories_totalCost").attribute_value("class")
Pestle answered 16/4, 2013 at 21:45 Comment(5)
Thank you, Chuck! That was a very important note about the .attribute_value I did not previously understand. Really appreciate you mentioning that.Eaton
For the css class, you can also use the Element#class_name method. For example, b.th(:id => "categories_totalCost").class_name.Fayina
when did we implement that? I don't see it listed in the rdoc rubydoc.info/gems/watir-webdriver/framesPestle
how do you use #attribute_value with a Regexp?Maidenhood
don't follow your question david.. a regular expression is it's own type of object in ruby, and does not have that method since it's not a watir object that inherits from Element.Pestle
P
7

This should do it:

browser.ths(:class => "disabledHeader").size
Psychedelic answered 16/4, 2013 at 18:12 Comment(2)
b.ths(:id, "categories_convertToSale").size ` => 1` b.ths(:class, "disabledHeader").size ` => 0`Eaton
Since the ID attribute is supposed to be unique within a page, we would normally expect any collection selected using ID to have a size of 1 unless you were using some kind of regular expression for the expected id value. Depending on what version of watir you are using, I think potentially for the second thing to work, you may need to use a regex there.. b.ths(:class, /disabledHeader/).sizePestle

© 2022 - 2024 — McMap. All rights reserved.