我正在尝试使用 Watir 将新文本设置到文本字段中。文本字段放置在 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">
文本字段有下一个 Xpath:
//*[@id="IDUserId"]
或 CSS 路径:
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
如果是 HTML,它看起来像这样:
<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>
我检查了很多变体都没有成功。例如:
browser.frame(:id => "frame_id_Login").text_field(:name => "IDUserId").set "admin"
返回:
/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)
有人可以帮我吗?我需要在字段中输入任何文本。
最佳答案
@browser.text_field(:id => "IDUserId").set "TEXT"
@browser.text_field(:name => "IDUserId").set "TEXT"
@browser.text_field(:class => "sws_text_input").set "TEXT"
这些都适用于此文本字段,只要它不包含在框架中,此时您需要识别它。
您只想将 Xpath 作为最后的结果使用,因为它脆弱且复杂。我什至不确定 CSS 是否有效,因为我从未尝试过。
请查看Željko's WATIR book或 www.watir.com用于基本定位器方法。
关于ruby - iframe 中的 Watir 和文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9560320/