1
votes

The case is the following: there are n panels on a page. Iterate through each panel: click on a specific element, then find the text of its child element which appears only after a click.

I can use methods like .has_css?, but that does not help me here.

Obviously it would be helpful to use find here, but it does not work on Capybara::Node::Element

page.all(:css, panelCss).each do |panel|
    counter+=1
    if panel.has_css?(elemCss)
        Actions.c 'Yay! Found #'+counter.to_s
        res = panel.find(:css, elemCss).text #**should be replaced**
    end
end

As far as I remember, I have done the same thing in past by combining capybara and jquery, but could not find the decision yet. Brief googling also did not help.

P.S. Of course, I can iterate directly through clickable elements, make a click and then get the text of a child element instead of iterating through their parent panels, but there are 2 reasons:

  • I need it for the future code
  • there could be more than 1 child element after clicking, so it seems I have to search within page.all node anyway.
1
Why do you say find doesn't work on Capybara::Node::Element? find should be callable on any element capybara returns and will scope the find to that element. Also what is Actions.c doing? - Thomas Walpole
@TomWalpole Thanks, of course you're right. It was just a silly "tired eye" mistake related to the following operation I was trying to perform with the found element. Actions.c prints a message to the report. - nrp

1 Answers

1
votes

From your description it should be something like

page.all(:css, panelCss).each do |panel|
  if panel.has_css?(elemToClickCss, wait: false) # assuming panels aren't dynamically loaded
    c = panel.find(:css, elemToClickCss)
    c.click
    res = c.find(:css, clickedObjectsChildCss).text
  end
end