tv4free
tv4free

Reputation: 287

watir print/put all visible links

$browser.links.each do |link|
          puts link.attribute_value("class")
end

How do I get all the visible/existing links in the put statement?

Upvotes: 2

Views: 1993

Answers (3)

confused1
confused1

Reputation: 253

If you require the class name of the links, you can use

$browser.links.each {|link| puts link.class_name if link.visible?}

or if you require any specific attribute of the link, you can use

$browser.links.each {|link| puts link.attribute_value("attribute_name") if link.visible?}

Upvotes: 0

Jarmo Pertman
Jarmo Pertman

Reputation: 1905

You can also write it by using shorter syntax like this:

puts $browser.links.find_all(&:present?).map(&:class_name)

Upvotes: 3

Željko Filipin
Željko Filipin

Reputation: 57262

This will output value of class attribute for all existing links on the page:

$browser.links.each {|link| puts link.attribute_value("class")}

This will output value of class attribute for all visible links on the page:

$browser.links.each {|link| puts link.attribute_value("class") if link.visible?}

Upvotes: 2

Related Questions