0
votes

Going off the example from the documentation http://www.rubydoc.info/gems/watir-webdriver/0.6.11/Watir/ElementCollection#each-instance_method, I am trying to click each element on the page that has the same class.
This is a code snippet of what I've come up with so far:

@b.divs(:class => 'portal-thumbnail-card').each do |div|    
    @b.div(:class => 'portal-thumbnail-card').click
    puts 'foo' 
    # my puts statement outputs 'foo' 6 times (matches the number of elements with that class)
    # right now this only clicks on the FIRST element, having issues with the other part :(    
end

Even though this doesn't involve any page reloading, are click actions possible?

3

3 Answers

1
votes

The problem is that you are locating the div to click during each iteration of the loop. In English, your code actually says, "for each div element with the class 'portal-thumbnail-card', click the first div on the page with class 'portal-thumbnail-card'."

What you actually want to do is click the div element that is the subject of each iteration:

@b.divs(:class => 'portal-thumbnail-card').each do |div|    
  div.click
  puts 'foo' 
end
1
votes

The divs method returns a Watir::DivCollection, which is a collection of Watir::Div objects. For example:

require 'watir-webdriver'
b = Watir::Browser.new 
b.goto('http://example.org')

divs = b.divs
puts divs.class
#=> Watir::DivCollection

divs.each { |d| puts d.class}
#=> Watir::Div

So--within your iterator--you want to refer to the block-local variable (i.e. div.click) instead of the browser's instance variable (i.e. @b.div(:class => 'portal-thumbnail-card').click)

0
votes

use flash method for see element what you try click

require 'watir-webdriver'

browser = Watir::Browser.new
browser.goto "data:text/html,#{DATA.read}"

browser.divs(:class => 'portal-thumbnail-card').each do |div|
#   browser.div(:class => 'portal-thumbnail-card').flash #you variant
    div.flash #correct variant
    puts 'foo'
end

browser.close

__END__

<html>
    <div class='portal-thumbnail-card'>
        <button id="button1">Button 1</button>
    </div>
    <div class='portal-thumbnail-card'>
        <button id="button2">Button 2</button>
    </div>
    <div class='portal-thumbnail-card'>
        <button id="button3">Button 3</button>
    </div>
    <div class='portal-thumbnail-card'>
        <button id="button4">Button 4</button>
    </div>
    <div class='portal-thumbnail-card'>
        <button id="button5">Button 5</button>
    </div>
</html>