Reputation: 55
I'm trying to tell Capybara/Cucumber to click on href but failed trying different options. Here's the code:
<div>
<h6 class="animation-visible tile-animation link-group color-white border-round bg-primary animated" data-animation-delay="1000" data-animation="cta-1">
<label>Option 1</label>
<a href="http://some.com/page1/">More</a>
</h6>
<h6 class="animation-visible tile-animation link-group color-white border-round bg-primary animated" data-animation-delay="1000" data-animation="cta-1">
<label>Option 2</label>
<a href="http://some.com/page2/">More</a>
</h6>
...
</div>
1st attempt was to have find_link('More').click the "link".
within(all(.animation-visible.tile-animation.link-group.color-white.border-round.bg-primary.animated>a).[0]) do
find_link('More').click
end
That didn't work. I've tried having it clicked straight away.
find(all('.animation-visible.tile-animation.link-group.color-white.border-round.bg-primary.animated>a')[0]).click
That didn't work either.
Just wondering if there's any other way to click Option 1? Or Option 2? These options need to be tested individually.
Upvotes: 1
Views: 2432
Reputation: 49950
Assuming the elements are visible on the page (not sure what the animation classes on the h6 elements are actually doing) then your first attempt wont work because of the extra > a on the selector in the within - which means you're finding the a already and then saying inside that find another a. Your second attempt won't work because find takes a selector type and selector (selector type defaults to css) to match against elements, but all returns an array of elements (you call either find to return one element or all to return multiple elements) - the following should work instead
within(all('.animation-visible.tile-animation.link-group.color-white.border-round.bg-primary.animated')[0]) do
find_link('More').click
end
which is saying inside the first .animation-visible... matching element find a link with 'More' for its text and click it. This could also be written
within(first('.animation-visible.tile-animation.link-group.color-white.border-round.bg-primary.animated', minimum: 1)) do
click_link('More')
end
That is basically the same thing except the minimum: 1 option will make it wait until at least one matching element is on the page (good for if your previous call is #visit
or the elements are loading via ajax
Note: you could use the minimum: 1 (or whatever number of elements you're expecting to appear on the page) on the first example too
The find_link
calls suggested in the other answer won't work because find_link doesn't take a css selector, it takes a string to match against the text, id, or title of the link. So the following would work
find_link('More', href: 'http://some.com/page1/').click
or
click_link('More', href: 'http://some.com/page1/')
Upvotes: 1
Reputation: 16201
Wouldn't these work?
First link
find_link("a[href$='page1/']").click
Second link:
find_link("a[href$='page2/']").click
Translation of the
css
selector: Find the link that ends with XXX
Upvotes: 0