UnknownObjectException – Unable to Locate Element

Have you tried to interact with an element, only to get an exception like:

Watir::Exception::UnknownObjectException: Unable to locate element, 
  using {:tag_name=>["div"]}
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.2.0/lib/watir-classic
      /element.rb:66:in `assert_exists'
    from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.2.0/lib/watir-classic
      /element.rb:125:in `text'
    from (irb):5
    from C:/Ruby193/bin/irb:12:in `<main>'

Watir is saying that it cannot find the element. Yet when you manually go and look at the page, you can see the element! Why can’t Watir locate it?

There are a variety of possible reasons, which have been summarized in the table below. Read the related pages (coming soon) to get more details on debugging/fixing the UnknownObjectException.

Problem Solution
Element is in a frame You must explicitly tell Watir that the element is within a frame.

Element has not finished loading Add an explicit wait.

Element locator uses dynamic attribute values Use a regex to only match the stable part of the attribute.

      #The following locator includes random values:
      browser.div(:id, 'static_part_randomnumber4651').text
      #Change the locator to a regex and just match the static part:
      browser.div(:id, /static_part/).text
Element is in a popup window You must explicitly tell Watir to use the popup window.

      browser.window(:title => /popup window title/) do
          browser.div(:id => 'div_in_popup').text
Element type is incorrect Verify that you are trying to find the right element type.

For example, what looks like a button might actually be a link.

Link Styled As Button

      #This will throw the UnknownObjectException

      #This will work

5 Responses to UnknownObjectException – Unable to Locate Element

  1. jack says:

    Hi Justin,

    What if I am using Watir both for desktop and mobile browsers. Getting the date picker pop up worked for in desktop browsers by using browser.frame/browser.iframe. But having problems when using emulated mobile browser for iPad/iPhone. Can’t access the elements inside the date picker control.

  2. mercelin says:

    I need to get the text present inside a frame(the elements in the frame are very dynamic). the code

    does not return any value

    returns only text outside the frame

    need a solution to retrieve the text inside the frame

    • Justin Ko says:

      driver.frame.text is likely returning an empty string because that is what the text of the frame element is. At least that is the opinion of the page that contains the frame element. There is nothing in Watir-Webdriver code that says to go into the frame itself to find the text. I am not sure if this is a bug, missing feature or known limitation.

      At any rate, the solution is to get the text of an element within the frame. To get all of the text, get the first element on the page, which will be the html element:

  3. betaslayer says:

    Thanks for the list. Kudos from India 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s