[Selenium and Ruby] Script not run while condition

Hi,

My scenario is:

  1. Access menu
  2. While icon “trash” exist, remove all
  3. If icon not exist, then add a new register

My code is:

require “selenium-webdriver”
require “rspec”
require ‘rspec/expectations’

describe “Current Expense” do

before(:all) do
@driver = Selenium::WebDriver.for :firefox
@base_url = “https://mysite.com
@driver.manage.window.maximize
end

after(:all) do
@driver.quit
end

it “Login in the system” do
@driver.get(@base_url)
@driver.find_element(:id, “user_email”).send_keys
[email protected]
@driver.find_element(:id, “user_password”).send_keys “123456”
@driver.find_element(:name, “commit”).click
@driver.find_element(:xpath, “//*[@href=‘/expenses/current’]”).click

wait = Selenium::WebDriver::Wait.new(:timeout => 5)
wait.until {@driver.find_element(:id => "expense_merchant")}

end

it “Remove and add new expense” do
begin

  • while(@driver.find_element(:xpath, "//[@class=‘i i-trashcan
    icon’]").displayed?)
  •  @driver.find_element(:xpath, "//*[@class='i i-trashcan
    

icon’]").click *

  •  sleep 2*
    
  •  @driver.find_element(:xpath, "//*[@class='btn 
    

btn-primary’]").click *

  • end *
    rescue Selenium::WebDriver::Error::NoSuchElementError
    @driver.find_element(:id, “expense_merchant”).send_keys “Taxi to
    work”
    @driver.find_element(:id, “expense_amount”).send_keys “50”
    @driver.find_element(:xpath, “//*[@type=‘submit’]”).click
    sleep 3
    end
    end
    end

I need that work WHILE only in the block in bold in the above code,
otherwise runs the rescue

My problem is that WHILE not stay in the loop, it runs only once and
then
run the rescue. Why?

Cheers,
Rafael