title: How to test whether an element is displayed on the page
body: <p>The following code checks whether an element is displayed and if the element is present runs a specific action, else the test continues normally:</p>
  
  <pre><code>require "selenium-webdriver"
  require "rspec"
  require 'rspec/expectations'
  
  
  describe "Current Expense" do
  
  
    before(:all) do
      @driver = Selenium::WebDriver.for :firefox
      @base_url = "http://the-internet.herokuapp.com/disappearing_elements"
      @driver.manage.window.maximize
    end
  
    after(:all) do
      @driver.quit   
    end
  
  
    it "Check icon" do
      @driver.get(@base_url)
      if expect(@driver.find_element(:xpath, "//*[@href='/gallery/']").displayed?).to be_truthy 
        @driver.find_element(:xpath, "//*[@href='/gallery/']").click
        sleep 2
        puts "element appears"
      else 
        puts "element NOT appears"
      end
    end
  end
  </code></pre>
  
  <p>When the element is present, the message appears, but when the element is not present in the page, an error occurs and the <code>else</code> block is not executed. What is causing this error?</p>