capybara - Clicking a button without an id

2019-06-28 03:41发布

问题:

I'm trying to to click the button in this html code

<div class="modal-footer"><button class="btn" data-dismiss="modal">Kapat</button></div>

I've already tried find with various combinations, the closest I came to success was with this code:

click_on "Kapat"

The problem is that there are 3 copies of the same button in the page, so my question is; is there a way to specify this particular div ?

回答1:

If the button has a specific path, you could use within or a find down to that path, but that path to the element would have to be unique in the page or you end up with the same problem (though, I believe using :xpath would give you a bit more flexibility here).

within ".modal-footer" do
  click_on "Kapat"
end

within ".another-selector" do
  click_on "Kapat"
end