4
votes

Here is the Watir code I am using:

require 'watir-classic'
browser = Watir::IE.new
browser.link(:class, "Wizardbutton").exists?

Here is a portion of the HTML of the page containing the link I am trying to check if exists.

<tr>
    <td align="left" style="vertical-align: top;">
        <a class="Wizardbutton"href="javascript:parent.showPopup('/web/wizard.html');window.focus();">
            <span>Add new Team</span>
        </a>
    </td>
</tr>

The error I am getting is:

Watir::Exception::UnknownObjectException: Unable to locate element, using {:tag_name=>["a"], :class=>"Wizardbutton"}

Why am I getting an error that the link element doesn't exist when I can clearly see it in the HTML source? I have successfully clicked on other links on the page but for some reason I cannot see this one. Is the embedded span tag messing something up? I have also tried to select using href and that didn't work either. Any insight would be greatly appreciated!

2
You probably need to offer a bit more information here. Is there anything on the page like JQuery which might play around with the DOM? Or perhaps the page hasn't loaded. Perhaps you could try the script again with a 10 second sleep, or disable javascript to see. - CBA

2 Answers

3
votes

OK the mystery is solved. Iframes are a major gotcha when working with Watir. Elements that are part of an iframe are not visible unless you specifically select the iframe and then select the item in the iframe. So for example the code

browser.frame(:name, "nameOfFrame").link(:class, "Wizardbutton")

means give me the iframe with the name attribute "nameOfFrame" and then select the link with the class attribute of "Wizardbutton".

2
votes

I am also faced same kind of errors. Some times, watir unable to find the browser control if it is present inside the div or tables tags. In your case, link control is placed inside the table. Please try this

browser.table(:class=> "classname").link(:class => "Wizardbutton")

Hope this will help you