Search code examples
capybara

click element if it exists in capybara


I wish to click a popup message that appears on my test app if it is present. I am new to capybara and cant seem to find a way to do this. I have previous experience with watir and if I were doing it with watir it would be something like:

if browser.link(:text, "name").exists? do
   browser.link(:text, "name").click
end

How can I do the same in capybara? Note this link will not always appear hence why I wish to have the if statement.


Solution

  • Have you tried doing something like:

    if page.find('.id') do 
       click_link('Some Link') # or page.find('.id').click
    else 
       page.should_not have_selector('.id') # or something like that
    end