I need to be able to execute code on part of the page within an ancestor of an unique element.
I have the following code:
find('div', text: 'text').ancestor('ancestor').find('desired').click
and I'm sometimes getting the following error on the second or third appearance of the above line in a test case:
Ambiguous match, found 2 elements matching visible css "desired"
within #<Capybara::Node::Element tag="html" path="/HTML">
This also happens more regularly when executing the following:
element = find('div', text: 'text').ancestor('ancestor')
element.find('desired').click
expect(element).to have_content('stuff')
I can not use first
or all
since I think they are the cause of stale element reference error on a dynamic page. The stale element error disappears by using this.
In the second example reinitiating element
after click does not help.
I don't know how to use ancestor with within
and even then I would have to use within every time I switch between this element and root, which is often.
Note that this is a potential duplicate of a unanswered question, but I created a new question because of the much newer version of capybara (I use 3.15.0) this is happening in. The issue is happening both locally and remote in this case.
EDIT: I found an example that always replicates the error:
element = find('example').ancestor(:css, '.ancestor')
element.find(:css, '.marked').click
puts element.path
expect(element).to have_no_css('.marked')
The path on line 3 will be correctly printed to console but the example will fail with the above error. If I put sleep 2
between puts ...
and expect ...
there will be no error. I suppose this is not expected capybara behaviour?