2500mA
2500mA

Reputation: 99

get text from each element

need to get amount text and check if it < 5000 then click next element in class

<div class="single sort">
    <div class="svariant front one" id="114_0">
        <img src="http://pics.avs.io/al_square/24/24/[email protected]" class="airlogo"    title="Turkish Airlines">
        <div class="amount">4 479
            <span class="rub">Р</span>
        </div>
      <a href="/travel/Order/Index/h7676Rn7471rdd?fid=349&amp;come=0" class="yellow front">BUY</a>
    </div>
</div>

<div class="single sort">
    <div class="svariant front one" id="113_0">
        <img src="http://pics.avs.io/al_square/24/24/[email protected]" class="airlogo"    title="Turkish Airlines">
        <div class="amount">9 479
            <span class="rub">Р</span>
        </div>
        <a href="/travel/Order/Index/h7676Rn7471rdd?fid=349&amp;come=0" class="yellow front">BUY</a>
    </div>
</div>

If I get value < 5 000 i clicked 'index +1'. How do this in cycle?

browser.div(:class => 'single sort', :index => 0).div(:class, 'amount').text

Formatting the text this way

sum = browser.div(:class => 'single sort', :index => 0).div(:class, 'amount').text
sum.gsub!(/[^0-9]/, '')
sum = sum.to_i

Upvotes: 0

Views: 816

Answers (3)

Justin Ko
Justin Ko

Reputation: 46846

Instead of finding the first one after the element that is less than 5000, I think it would be easier to find the first element that is 5000 or greater. Looking at the problem this way, you can simply create an element collection of the divs. Then find the first one where the text matches the specified condition.

match = browser.divs(class: 'single sort').find do |div|
    div.div(class: 'amount').text.tr('^0-9', '').to_i >= 5000
end
match.link(class: 'yellow front').click

If you prefer to find the first one after the one that is less than 5000, you could use the drop_while method instead. After dropping the ones that do not meet the criteria, the first element in the collection will be greater than the 5000.

matches = browser.divs(class: 'single sort').drop_while do |div|
    div.div(class: 'amount').text.tr('^0-9', '').to_i < 5000
end
matches.first.link(class: 'yellow front').click

Upvotes: 1

Sveatoslav
Sveatoslav

Reputation: 823

here is more universal way, for any range of given elements, without the hardcoded (0..1), be there 2 elements, or 324834 :)

b.divs(:class => 'single sort').each {|x| if x.text.gsub!(/\D/, "").to_i<5000; puts x.text; else; puts "The value is #{x.text}"; end}

Upvotes: 1

orde
orde

Reputation: 5283

You could create a Range and then iterate over it, which allows for incrementing the index. Here's a hacky, contrived example:

(0..1).each do |i|
  amount =  b.div(:class => 'single sort', :index => i).div(:class, 'amount').text.gsub!(/\D/, "")
  amount = amount.to_i
  next if amount < 5000
  puts amount               #=> 9479
end

Since there are no <a> tags in the HTML snippet, it's not clear what should be clicked, but you could potentially replace puts amount with the click action.

Upvotes: 1

Related Questions