Instead of working from the the "gc-list", you could work from the 
"gift-cards-list". It would save a level of nesting and eliminate the child 
vs descendant difference:

# Page-Object
divs(:applied_gift_card, :class => 'gift-cards-list')

# Usage
p page.applied_gift_card_elements[0].div_element(index: 0).text
#=> "...163867647149"
p page.applied_gift_card_elements[0].div_element(index: 1).text
#=> "$20.00"

For readability, I would suggest taking it a step further and using page 
sections:

class MyPage
  include PageObject

  class AppliedGiftCard
    include PageObject

    div(:card, index: 0)
    div(:amount_taken, index: 1)
  end

  page_sections(:applied_gift_cards, AppliedGiftCard, class: 
'gift-cards-list', tag_name: 'div')
end

page = MyPage.new(browser)
p page.applied_gift_cards[0].card
#=> "...163867647149"
p page.applied_gift_cards[0].amount_taken
#=> "$20.00"
p page.applied_gift_cards[1].card
#=> "...169169063156"
p page.applied_gift_cards[1].amount_taken
#=> "$25.90\n($74.10 left on card)"

Justin


On Thursday, November 22, 2018 at 11:44:34 PM UTC-5, NaviHan wrote:
>
> Oops my understanding was wrong here :-(
>
> I can achieve this by using 
>
> return applied_gift_cards_element.children[index.to_i].children[0].text
>
> Is there a a better way?
>
>
> On Friday, 23 November 2018 13:06:07 UTC+11, NaviHan wrote:
>>
>> Having trouble using index.
>>
>> I have a section of a page as below.
>> <div class="gc-list">
>>     <div class="row cards-list-titles" style="display: flex;">
>>         <div class="small-6 large-5">Card</div>
>>         <div class="small-5">Amount Taken</div>
>>     </div>
>>     <div class="row gift-cards-list" data-gcid="2790030163867647149" 
>> style="display: flex;">
>>         <div class="small-6 large-5">...163867647149</div>
>>         <div class="small-5 large-6">$20.00</div>
>>         <div class="small-1 large-1 small-text-right">
>>             <button type="button" class="remove-gift-cert pointer" 
>> data-gcid="2790030163867647149">
>>                 <span class="icon icon-cross-standard-small-black"
>> ></span>
>>             </button>
>>         </div>
>>     </div>
>>     <hr class="gc-separator" style="display: block;">
>>         <div class="row gift-cards-list" data-gcid="2790030169169063156" 
>> style="display: flex;">
>>             <div class="small-6 large-5">...169169063156</div>
>>             <div class="small-5 large-6">$25.90
>>                 <br class="hide-for-large">
>>                     <span class="gc-amount-left">($74.10 left on card)
>> </span>
>>                 </div>
>>                 <div class="small-1 large-1 small-text-right">
>>                     <button type="button" class="remove-gift-cert 
>> pointer" data-gcid="2790030169169063156">
>>                         <span class="icon 
>> icon-cross-standard-small-black"></span>
>>                     </button>
>>                 </div>
>>             </div>
>>             <hr class="gc-separator" style="display: block;">
>>             </div>
>>
>>
>> The pageobject is defined as
>>
>> div(:applied_gift_cards, :class => 'gc-list')
>>
>>
>>
>>
>> And Im trying to access "...163867647149 " and "$20.00" as which gave 
>> error
>>
>> return applied_gift_cards_element.div_element(:index => 
>> 1).div_element(:index =>0).text
>>
>> return applied_gift_cards_element.div_element(:index => 
>> 1).div_element(:index =>0).text
>>
>> Watir::Exception::UnknownObjectException: timed out after 30 seconds, 
>> waiting for #<Watir::Div: located: false; {:class=>"gc-list", 
>> :tag_name=>"div"} --> {:index=>1, :tag_name=>"div"} --> {:index=>0, 
>> :tag_name=>"div"}> to be located
>>
>> When printing the same element actually prints the Pageobect
>> puts applied_gift_cards_element.div_element(:index => 1).div_element(:index 
>> =>0)
>> #<PageObject::Elements::Div:0x0000000003b66388>
>>
>>
>> A puts of the following printed as below which is again confusing
>>
>> puts applied_gift_cards_element.div_element(:index => 0).inner_html
>> puts applied_gift_cards_element.div_element(:index => 1).inner_html
>> puts applied_gift_cards_element.div_element(:index => 2).inner_html
>> puts applied_gift_cards_element.div_element(:index => 3).inner_html
>>
>> <div class="small-6 large-5">Card</div>
>>  <div class="small-5">Amount Taken</div>
>> Card
>> Amount Taken
>> <div class="small-6 large-5">...163867647149</div><div class="small-5 
>> large-6">$20.00</div><div class="small-1 large-1 small-text-right"><button 
>> type="button" class="remove-gift-cert pointer" 
>> data-gcid="2790030163867647149"><span class="icon 
>> icon-cross-standard-small-black"></span></button></div>
>>
>>
>>
>>
>> Am I doing something wrong here?
>>
>>

-- 
-- 
Before posting, please read 
https://github.com/watir/watir_meta/wiki/Guidelines-for-Posting-to-Watir-General-Google-Group.
 
In short: search before you ask, be nice.

[email protected]
http://groups.google.com/group/watir-general
[email protected]
--- 
You received this message because you are subscribed to the Google Groups 
"Watir General" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to