Alexander Stavonin Alexander Stavonin - 11 months ago 85
Ruby Question

Watir and text field inside iframe

I'm trying to setup new text into a text field using Watir. The text field is placed inside iframe.

<div id="div_popup_layer" style="width: 100%; height: 100%;">
<div id="id_Login" class="ui-draggable" style="position:absolute;left:372.5px;top:279px;width:501px;height:274px;z-index:15">
<table id="popup_cont_id_Login" cellspacing="0" cellpadding="0" topmargine="0">
<tbody>
<tr>
<td align="left" valign="middle">
<iframe id="frame_id_Login" height="274px" frameborder="0" width="501px" allowtransparency="true" src="/sws.login/gnb/loginView.sws?basedURL=undefined&popupid=id_Login" tagtype="popup_iframe" name="frame_id_Login">


The text field has next Xpath:

//*[@id="IDUserId"]


Or CSS Path:

html body#POPUP_BODY table#loginBackground tbody tr td form#loginForm table tbody tr td table tbody tr td table tbody tr td div#userIDBlock table tbody tr td table tbody tr td#swsText_body_IDUserId_td.sws_text_normal_body div input#IDUserId.sws_text_input


And in case of HTML it looks like this:

<div><input type="text" value="" maxlength="63" style="width:142px; " class="sws_text_input" name="IDUserId" id="IDUserId" onfocus=" swsText_styleChangedFocus('IDUserId');" autocomplete="off" tagtype="text"></div>


I've checked a lot of variants without success. For example:

browser.frame(:id => "frame_id_Login").text_field(:name => "IDUserId").set "admin"


Returns:

/var/lib/gems/1.8/gems/watir-webdriver-0.5.3/lib/watir-webdriver/elements/frame.rb:9:in `locate': unable to locate frame/iframe using {:id=>"frame_id_Login"} (Watir::Exception::UnknownFrameException)


Could anyone help me with it? I need to put any text into the field.

Answer
@browser.text_field(:id => "IDUserId").set "TEXT"
@browser.text_field(:name => "IDUserId").set "TEXT"
@browser.text_field(:class => "sws_text_input").set "TEXT"

Those will all work for a this text field as long as it is not contained in a frame, at which point you will need to identify it.

You only want to use Xpath as a last result, as it is fragile and complicated. I'm not even sure if CSS works as I've never tried it.

Please check out Željko's WATIR book or www.watir.com for basic locator methods.