Dashboard > Watir > ... > Complex > Frames
Watir Log In View a printable version of the current page.
Frames
Added by Zeljko, last edited by Bret Pettichord on Jun 30, 2008  (view change)
Labels: 
(None)

Frames

Many web applications use frames. If you are having trouble accessing objects on a web page, be sure to check to see if the application is using frames. To find out, view the source of the page and look for <frame> or <iframe> HTML tags. It will look something like this:

<frameset cols="*,*">
  <frame src="menu.htm" name="menu">
  <frame src="main.htm" name="main">
</frameset>

In the HTML above, we have a menu frame on the left and a main frame on the right.

You can use Watir to find out if your page has frames:

ie.show_frames

This is a good method to use with IRB. It prints out the index and the name of the frames in the current page. Like this:

irb(main):009:0> ie.show_frames
there are 2 frames
frame  index: 1 name: menu
frame  index: 2 name: main
=> 0..1

Watir allows access to frame objects by identifying them by the attributes available in the <frame> HTML tag or by frame's index. Common attributes are id, name and src. For complete list see Methods Supported by Element.

name Attribute

Watir code to access a frame using name attribute:

ie.frame(:name, "menu")

To access individual objects within that frame, you prefix the object using the Watir code above. If we had a link in the menu frame:

<a href="index.htm">Click Menu Item</a>

we could click it like this:

ie.frame(:name, "menu").link(:text, "Click Menu Item").click

Nested Frames

Sometimes a web page that is referenced by the <frame> tag is also a page that contains a <frameset> tag. This is a nested frame. To deal with this, simply access the nested frame like this:

ie.frame(:name, "frame").frame(:name, "nested_frame")

Why do I get an access denied error when trying to access a frame?

This error message is due to Internet Explorer's attempt to prevent cross-window domain scripting (also known as cross-site scripting, or XSS).  For security reasons, IE prevents embedded code in one frame from accessing another if the frame contents come from different domains. Watir runs into the same barriers when you attempt to navigate from a page hosted from one site to a frame hosted by another.

You may try one or more of the following methods to resolve this issue:

  1. Navigate directly to the subframe (and the server that hosts it).  That is, instead of having your script click on the link, use ie.goto("http://www.thenewsite.com/framecontents.html").
  2. Add the particular host to the Internet Explorer Trusted Sites list.  From IE's menu bar, choose Tools / Internet Options; click on the Security tab; click on the Trusted Sites icon; click on the Sites... button; type the name of the site into the field labelled "Add this Web site to the zone:".  You may need to uncheck the box labelled "Require server verification ( https: ) for all sites in this zone."  Click OK to finish the process.
  3. Create an alias in the hosts file.  This text file is typically in the folder c:\windows\system32\drivers\etc.  Add a line in the form
    192.168.10.32 foosystem

    Replace 192.168.10.32 with the IP address of the host that is serving up the frame contents.  Then access the frame using https://foosystem/testsystem.

  4. Set Internet Explorer to its lowest possible security setting.  From IE's menu bar, choose Tools / Internet Options; click on the Security tab; click on the Default Level button, and slide the slider to the Low setting; then click on OK to finish the process.

Watir 1.4 If none of these work, and you are still getting annoying error messages about Access Denied when your scripts run, you can turn off these warnings. 

ie.logger.level = Logger::ERROR

Microsoft Reference: For background information about the security rules that cause this problem, see About Cross-Frame Scripting and Security

Go to contents, previous, next page.

Site running on a free Atlassian Confluence Community License granted to OpenQA. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.6 Build:#812 Aug 06, 2007) - Bug/feature request - Contact Administrators