因为我的 B593 路由器有时会卡在它产生大约 50% 数据包丢失的状态,所以我正在尝试编写一个程序,让它在最后 100 次 ping 中有 30 次丢失时自动重启。
我无法单击实际的重新启动按钮本身,它似乎是由一些 Javascript 加载的,但 watir 无法看到该按钮。在谷歌上搜索了一下,似乎我需要提供我目前正在尝试做的这个按钮的路径 - 但到目前为止还没有运气。
如果有人能提供一些启发,那真的会让我和其他一些 B593 所有者感到高兴。
当我在重启按钮上单击“复制 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"> </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> </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>
</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>'