Ruby,Watir,无法单击Java Script加载的Frame中的按钮

时间:2013-04-19 03:47:07

标签: javascript ruby watir watir-webdriver

因为我的B593路由器有时会停留在产生大约50%丢包率的状态下我试图写一个程序让它自动重启,如果让我们说最后100个ping 30丢失了。

我无法点击实际的重启按钮本身,它似乎被一些Javascript加载但是watir无法看到按钮。谷歌搜索相当多,似乎我需要提供这个按钮的路径,我目前正在尝试 - 但到目前为止没有运气。

如果有人能说清楚它会让我和其他一些其他B593的主人成为现实。

这是google检查员在重启按钮上点击“复制XPath”时所说的内容。

//*[@id="btnReboot"]

这是重启按钮在帧源代码中的外观。

<button onclick="reboot()" id="btnReboot">
<script>document.write(gVarReboot);</script>Reboot
</button>

页面源代码

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>content</title>
<script language='javascript'>
var REFRESH_TIME = 60000;
var ssidIdx = 0;
var lanDevIndex = 0;
var dhcpOptRedio = 0;
var QoSCurInterface = '';
var DDNSProvider = '';
var ripIndex = "";
var previousPage = "";
var preAddDomain = "";
var editIndex = -1;
var editDomain = '';
var g_FWLevel = -1;
var g_LevelOptRedio = 0;
var g_oMenuTree = null;
var curUserType = "0";
var sptUserType = '0';
var sysUserType = '1';
var aplyLine = -1;
var Link = "/html/help/help_content.html#";
var x = parseInt(screen.width / 2);
var y = parseInt(screen.height / 2);
var g_url = '';
var g_firstmenu = 'Admin_0';
var g_secondmenu = 'Admin_0_0';
var g_thirdmenu = 'Admin_0_0_0';
var errStrStr = '';
var errIdCtrlStr = '';
var updateIncoming = 0;
var g_IsVpnCertsUploaded = 0;
var g_is_refresh = true;
var g_modifiedFlg = false;
var ipfiltertablelist = null;
var g_wlanBasicSetting = "1";
var g_expendUpgDiv = true;
var g_smsType='0';
var g_isFromPinPage = false;
function addTimer() {
setInterval("resetModifiedFlg();", REFRESH_TIME);
}
function resetModifiedFlg() {
try {
if (top.g_modifiedFlg) {
top.g_modifiedFlg = false;
top.frames["hiddenfrm"].location
.replace("/html/main/hidden.asp");
}
} catch (e) {
var forjslint = 0;
}
}
</script>
<script language="JavaScript">
</script>
<frameset rows='68,*,20' border='0' frameborder='no' framespacing='0'
onload="addTimer();">
<frameset cols='0,0,0,*' frameborder='0' border='0'>
<frame src='/html/main/refresh.asp' name='connfrm' id='connfrm'
frameborder='no' border='0' scrolling='no'
marginwidth='0' marginheight='0' noresize></frame>
<frame src='/html/main/hidden.asp' name='hiddenfrm'
id='hiddenfrm' frameborder='no' border='0' scrolling='no'
marginwidth='0' marginheight='0' noresize></frame>
<frame src='' name='helpfrm' id='helpfrm' frameborder='no'
border='0' scrolling='no' marginwidth='0'
marginheight='0' noresize></frame>
<frame src='/html/main/logo.asp' name='logofrm' id='logofrm'
frameborder='no' border='0' scrolling='no'
marginwidth='0' marginheight='0'></frame>
</frameset>
<frameset cols='200,*,0' frameborder='0' border='0'>
<frame src='/html/main/menu.asp' name='menufrm' id='listfrm'
frameborder='no' border='0' scrolling='no'
marginwidth='0' marginheight='0' noresize></frame>
<frame src='' name='contentfrm' id='contentfrm' frameborder='0'
border='0' scrolling='auto' marginwidth='0'
marginheight='16' noresize></frame>
<frame src='' name='uploadFrm' id='uploadFrm'
frameborder='no' border='0' scrolling='no'
marginwidth='0' marginheight='0' noresize></frame>
</frameset>
<frame src='/html/main/footer.html' name='footerfrm'
frameborder='no' border='0' scrolling='no'
marginwidth='0' marginheight='0' noresize></frame>
</frameset>
<noframes>
The information on the page makes use of frame technology. For best
results, use the new version of Browser.
</noframes>
</head>
</body>
</html>

框架源代码

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="/js/loadbyver.js?ver=V100R001C56SP052"></script>
<script type="text/javascript">
loadCss("/css/newcommon.css");
loadCss("/css/contentstyle.css");
loadCss("/lang/lang.css");
loadRes("/lang/reset.res");
loadJs("/js/util/base.js");
loadJs("/js/util.js");
loadJs("/js/tabinfo.js");
loadJs("/js/reset.js");
</script>
</head>
<body>
<div class="page_container"><div id="page_path" class="page_path">&nbsp;</div>
<table cellspacing=0 cellpadding=0 class="layout_table">
<tr>
<td class="layout_top_gap">
</td>
</tr>
<tr>
<td>
</td>
<td class="layout_center_td">
<table cellspacing=0 cellpadding=0 class="layout_title_table" id="layoutPageTitleId">
<tr>
<td class="layout_title_td">
<script type="text/javascript">
document.write(gVarReset);
</script>
</td>
</tr>
</table>
<table class="page_all_table" cellspacing="0" cellpadding="0">
<tr class="page_all_tr">
<td class="page_td_gap_left"></td>
<td class="page_all_content_td" id="page_all_content_td">
<table cellspacing=0 cellpadding=0 class="page_top_discription" id="pageTopDiscriptionId" >
<tr>
<td id="pageTopDiscriptionTdId" class="page_op_iscription_td">
<script type="text/javascript">
document.write(gVarPageComment);
</script>
</td>
</tr>
</table>
<div class="module_container">
<table cellspacing="0" cellpadding="0" class="module_header">
<tr>
<td id="id_rebootTitle">
<div class="module_header_title">
<div class="module_header_title_bg_left"></div>
<div class="module_header_title_text"><script>
document.write(gVarReboot);
</script></div>
<div class="module_header_title_bg_right"></div>
</div>
</td>
</tr>
</table>
<table id="id_rebootBody" cellspacing="0" cellpadding="0"
class="module_content" >
<tr>
<td style="border:none" align="left">
<div  style="margin-left:15px; margin-top : 15px;">
<script language="JavaScript" type="text/javascript">
document.write(gVarClickrebootb);
</script>
</div>
</td>
</tr>
</table>
<table cellspacing="0" cellpadding="0" class="module_bottom">
<tr>
<td>
<div>
<button onclick="reboot()" id="btnReboot">
<script>document.write(gVarReboot);</script>
</button>
</div>
</td>
</tr>
</table>
<table cellspacing="0" cellpadding="0" class="module_header">
<tr>
<td id="id_restoreTitle">
<div class="module_header_title">
<div class="module_header_title_bg_left"></div>
<div class="module_header_title_text">
<script>
document.write(gVarRestoreDefalut);
</script>
</div>
<div class="module_header_title_bg_right"></div>
</div>
</td>
</tr>
</table>
<table id="id_restoreBody" cellspacing="0" cellpadding="0"
class="module_content" >
<tr>
<td style="border:none" align="left">
<div  style="margin-left:15px; margin-top : 15px;">
<script language="JavaScript" type="text/javascript">
document.write(gVarClickRestoreD);
</script>
</div>
</td>
</tr>
</table>
<table cellspacing="0" cellpadding="0" class="module_bottom">
<tr>
<td>
<div>
<button onclick="restoreDefalut()" id="btnResory">
<script>document.write(gVarRestoreDefalut);</script>
</button>
</div>
</td>
</tr>
</table>
</div>
<div>&nbsp;</div>
<div id="erroinfo"  style="width:100%; display:none"></div>
</td>
<td style="width:10px;"></td>
</tr>
</table>
<table cellspacing="0" cellpadding="0" class="module_bottom">
<tr>
<td class="layout_left_corner">
</td>
<td>
&nbsp;
</td>
<td  class="layout_right_corner">
</td>
</tr>
</table>
</td>
<td class="layout_center_gap">
</td>
<td class="page_td_help_layout">
<div class="layout_help_top_div" id="id_layout_help_top_div"></div>
<div style=" padding:10px;">
<div id="page_all_help_td" ></div>
</div>
</td>
<td class="layout_right_gap">
</td>
</tr>
</table>
</div>
</body>
<script>
initPage();
__init();
</script>
</html>

我当前的代码

require "watir-webdriver"

admin_password = 'stackoverflowcanhelp'

#spawn instance
browser = Watir::Browser.new :chrome

#open homepage
browser.goto "http://192.168.0.75"

#set Langauge to english
browser.select_list(:name => 'Language').select 'English'

#enter password
browser.text_field(:name => "Password").set admin_password

#login
browser.a(:id, "btnLogin").click

#go to reboot menue
browser.cookies.add 'FirstMenu', 'Admin_10', :path => "/"
browser.cookies.add 'SecondMenu', 'Admin_10_1', :path => "/"
browser.cookies.add 'ThirdMenu', 'Admin_10_1_0', :path => "/"

#refresh browser
browser.refresh

#reboot router
browser.button(:xpath, '//*[id="btnReboot"]').click #does not work
browser.alert.set "Click "
browser.alert.ok

#close browser
browser.close

错误消息

gems/watir-webdriver-0.6.2/lib/watir-webdriver/elements/element.rb:490:in `assert_exists': unable to locate element, using {:xpath=>"//*[id=\"btnReboot\"]", :tag_name=>"button"} (Watir::Exception::UnknownObjectException)
    from /Users/stackoverflowfan/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/gems/watir-webdriver-0.6.2/lib/watir-webdriver/elements/element.rb:118:in `click'
    from a.rb:29:in `<main>'

2 个答案:

答案 0 :(得分:2)

我找到了另一种方法,但仍然对xpath的方式感兴趣。 :)

browser.frame(:id => "contentfrm").button(:id => "btnReboot").when_present.click

答案 1 :(得分:1)

另一种方法是直接调用“rebo​​ot()”函数。

以下是:

   require "watir"

   ie = Watir::IE.new()
   ie.goto("address")
   ie.execute_script("reboot()")

这直接调用了所需的功能,这样可以省去在页面上找到疯狂事物的麻烦。

祝你好运