Search code examples
rubywatirwatir-webdriver

How to handle Watir::Exception::UnknownObjectException:


I'm using watir to automate the deleting of elements using a while loop, but when the scripts reaches the end it errors out since the element that the while loop is dependent on is no longer visible. Is there anyway to have Watir carry on with the test?

Here is an example of my code:

class Delete_element

  def org_roster_remove
    parameters = Tt_parameters.new
    driver = Login.new.log_in(parameters.username3, parameters.password3)
    while driver.a(:data_class, 'home.group.edit_group_btn').visible? == true
      driver.p(:data_class, 'home.conv.messagePreview').hover
      sleep(1)
      driver.div(:class, 'delete').click
      sleep(1)
      driver.a(:data_class, 'home.conv.deleteFromRoster_btn').click
      sleep(1)
    end
    driver.quit
  end
end

This is the error:

Watir::Exception::UnknownObjectException: unable to locate element, using {:data_class=>"home.group.edit_group_btn", :tag_name=>"a"}


Solution

  • I would use .present? instead of .visible?

    Also consider using .wait_until_visible instead of sleeping. See Watir-webdriver Waiting.

    Additionally, investigate a page object framework such as Cheezy's page-object gem