Reputation: 5735
I am trying to click all the links within a table. The xpath is below.
(2..lines).each do |i|
browser.element(:xpath => "//*[@id=\"StmtSummaryForm:stmtDataTabletbody0\"]/tr[{i}]/td[3]/a").click
end
However, I get
NoMethodError: undefined method `click' for #<Watir::HTMLElementCollection:0x00007fcfa69d3c08>
I assume my xpath is wrong, or I'm using watir incorrectly.
PRY:
[61] pry(main)> elements = @browser.elements(:xpath => "//*[@id=\"StmtSummaryForm:stmtDataTabletbody0\"]/tr[2]/td[3]/a")
=> #<Watir::HTMLElementCollection:0x00007fcfa7424130
@query_scope=#<Watir::Browser:0x..f0106c8d0f94f382 url="https://personal.domain.com/us/Statements" title="Statements">,
@selector={:xpath=>"//*[@id=\"StmtSummaryForm:stmtDataTabletbody0\"]/tr[2]/td[3]/a"},
@selector_builder=
#<Watir::Locators::Element::SelectorBuilder:0x00007fcfa6b8fb78
@built={:xpath=>"//*[@id=\"StmtSummaryForm:stmtDataTabletbody0\"]/tr[2]/td[3]/a"},
@custom_attributes=[],
@query_scope=#<Watir::Browser:0x..f0106c8d0f94f382 url="https://personal.domain.com/us/Statements" title="Statements">,
@selector={:xpath=>"//*[@id=\"StmtSummaryForm:stmtDataTabletbody0\"]/tr[2]/td[3]/a"},
@valid_attributes=
[:title,
:lang,
:dir,
:dataset,
:accesskey,
:accessKey,
:innertext,
:innerText,
:onabort,
:onblur,
:oncancel,
:oncanplay,
:oncanplaythrough,
:onchange,
:onclick,
:onclose,
:oncuechange,
:ondblclick,
:ondrag,
:ondragend,
:ondragenter,
:ondragexit,
:ondragleave,
:ondragover,
:ondragstart,
:ondrop,
:ondurationchange,
:onemptied,
I also tried:
[68] pry(main)> elements = @browser.elements(:xpath => "//*[@id=\"StmtSummaryForm:stmtDataTabletbody0\"]/tr[2]/node()").links
/Users/aa/.rvm/gems/ruby-2.7.1/gems/pry-0.12.2/lib/pry/exceptions.rb:28: warning: $SAFE will become a normal global variable in Ruby 3.0
NoMethodError: undefined method `links' for #<Watir::HTMLElementCollection:0x00007fcfa744e610>
Upvotes: 1
Views: 125
Reputation: 6064
You are calling click
on collections elements
, collection doesn't have click method. You have to iterate the collection and then have to click one after another.
Write this code
browser.table(id: "StmtSummaryForm:stmtDataTabletbody0").rows.each do |row|
row.cell(index: 2).link.click
end
It will click the link which is on the third column of the table. WATIR automatically calculates the total number of rows in the table, you don't have to specify the count explicitly like you have done.
Upvotes: 1
Reputation: 4194
You shouldn't get an HTMLElementCollection
error if you are using browser.element
.
Watir is designed to reduce reliance on XPath in general. It can get complicated if the clicks reload the page; especially if the reload might have a different structure. Regardless, this *should work:
table = browser.table(id: "StmtSummaryForm:stmtDataTabletbody0")
table.links.each(&:click)
Upvotes: 1