test.js
var ie = WScript.createobject("InternetExplorer.Application");
var eoms1 = "http://xxx.com/url1/";
var eoms2 = "http://xxx.com/url2/";
ie.Navigate(eoms1);
SynchronizeIE();//等待ie加载完成
var doc = ie.document;
var bb = doc.getElementsByName("submit");
doc.forms[0].j_username.value = "xxxxx";
doc.forms[0].j_password.value = "xxxxx";
bb[0].click();
//上面自动登陆该网站,可正确执行SynchronizeIE();//等待ie加载完成
ie.Navigate(eoms2);
SynchronizeIE();//等待ie加载完成
var doc2 = ie.document;
ie.Visible = true;
SynchronizeIE();//等待ie加载完成
var dutyDOM = doc2.getElementById("dutymain").getElementById("topbar1").getElementsByTagName("td");
这里出错,提示doc2.getElementById()为空或不是对象!
dutyDOM[1].fireEvent("onclick");
SynchronizeIE();function SynchronizeIE() { //等待ie加载完成
while(ie.Busy) {
WScript.Sleep(100);
}
}xxx.com/url2查看到的源代码:<DIV id=dutymain><TABLE width="100%" border=0
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="http://mycompany.com/mynamespace"
xmlns:xslt="http://www.w3.org/TR/WD-xsl">
<TBODY>
<TR>
<TD vAlign=top>
<DIV id=toolBar>
<TABLE id=topbar1
style="BORDER-RIGHT: 1px outset; BORDER-TOP: 1px outset; FONT-SIZE: 12pt; BORDER-LEFT: 1px outset; BORDER-BOTTOM: 1px outset"
borderColor=#f5f5f1 height=35 cellSpacing=2 cellPadding=0 width=120
bgColor=#c0cef3 border=0>
<TBODY>
<TR>
<TD onmousedown="this.style.border='2 #cccccc inset';"
onmouseover="this.style.border='2 #eeeeee outset';this.style.cursor='hand'"
style="BORDER-RIGHT: #ffffff 2px; BORDER-TOP: #ffffff 2px; BORDER-LEFT: #ffffff 2px; CURSOR: hand; BORDER-BOTTOM: #ffffff 2px"
onclick=self.print(); onmouseout="this.style.border='2 #ffffff'" vAlign=center
noWrap><IMG src="http://xxx.com/url11/images/print.gif" border=0><FONT
color=darkblue>打印</FONT></TD></TR>
<TR class=topbar vAlign=center align=left>
<TD onmousedown="this.style.border='2 #cccccc inset';"
onmouseover="this.style.border='2 #eeeeee outset';this.style.cursor='hand'"
style="BORDER-RIGHT: #ffffff 2px; BORDER-TOP: #ffffff 2px; BORDER-LEFT: #ffffff 2px; CURSOR: hand; BORDER-BOTTOM: #ffffff 2px"
onclick="getDutyWork('DUTY_WORK_CHECK')"
onmouseout="this.style.border='2 #ffffff'" vAlign=center noWrap><IMG
src="http://xxx.com/url11/images/filesselected.gif" border=0><FONT
color=darkblue>巡检记录</FONT></TD></TR>
<TR>
<TD onmousedown="this.style.border='2 #cccccc inset';"
onmouseover="this.style.border='2 #eeeeee outset';this.style.cursor='hand'"
style="BORDER-RIGHT: #ffffff 2px; BORDER-TOP: #ffffff 2px; BORDER-LEFT: #ffffff 2px; CURSOR: hand; BORDER-BOTTOM: #ffffff 2px"
onclick="getDutyWork('DUTY_WORK_AFFAIR')"
onmouseout="this.style.border='2 #ffffff'" vAlign=center noWrap><IMG
src="http://xxx.com/url11/images/menu_link_default.gif" border=0><FONT
color=darkblue>事务记录</FONT></TD></TR>
.....下面还有
代码里div的id明明是dutymain,为什么getElementById()找不到呢?
var ie = WScript.createobject("InternetExplorer.Application");
var eoms1 = "http://xxx.com/url1/";
var eoms2 = "http://xxx.com/url2/";
ie.Navigate(eoms1);
SynchronizeIE();//等待ie加载完成
var doc = ie.document;
var bb = doc.getElementsByName("submit");
doc.forms[0].j_username.value = "xxxxx";
doc.forms[0].j_password.value = "xxxxx";
bb[0].click();
//上面自动登陆该网站,可正确执行SynchronizeIE();//等待ie加载完成
ie.Navigate(eoms2);
SynchronizeIE();//等待ie加载完成
var doc2 = ie.document;
ie.Visible = true;
SynchronizeIE();//等待ie加载完成
var dutyDOM = doc2.getElementById("dutymain").getElementById("topbar1").getElementsByTagName("td");
这里出错,提示doc2.getElementById()为空或不是对象!
dutyDOM[1].fireEvent("onclick");
SynchronizeIE();function SynchronizeIE() { //等待ie加载完成
while(ie.Busy) {
WScript.Sleep(100);
}
}xxx.com/url2查看到的源代码:<DIV id=dutymain><TABLE width="100%" border=0
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="http://mycompany.com/mynamespace"
xmlns:xslt="http://www.w3.org/TR/WD-xsl">
<TBODY>
<TR>
<TD vAlign=top>
<DIV id=toolBar>
<TABLE id=topbar1
style="BORDER-RIGHT: 1px outset; BORDER-TOP: 1px outset; FONT-SIZE: 12pt; BORDER-LEFT: 1px outset; BORDER-BOTTOM: 1px outset"
borderColor=#f5f5f1 height=35 cellSpacing=2 cellPadding=0 width=120
bgColor=#c0cef3 border=0>
<TBODY>
<TR>
<TD onmousedown="this.style.border='2 #cccccc inset';"
onmouseover="this.style.border='2 #eeeeee outset';this.style.cursor='hand'"
style="BORDER-RIGHT: #ffffff 2px; BORDER-TOP: #ffffff 2px; BORDER-LEFT: #ffffff 2px; CURSOR: hand; BORDER-BOTTOM: #ffffff 2px"
onclick=self.print(); onmouseout="this.style.border='2 #ffffff'" vAlign=center
noWrap><IMG src="http://xxx.com/url11/images/print.gif" border=0><FONT
color=darkblue>打印</FONT></TD></TR>
<TR class=topbar vAlign=center align=left>
<TD onmousedown="this.style.border='2 #cccccc inset';"
onmouseover="this.style.border='2 #eeeeee outset';this.style.cursor='hand'"
style="BORDER-RIGHT: #ffffff 2px; BORDER-TOP: #ffffff 2px; BORDER-LEFT: #ffffff 2px; CURSOR: hand; BORDER-BOTTOM: #ffffff 2px"
onclick="getDutyWork('DUTY_WORK_CHECK')"
onmouseout="this.style.border='2 #ffffff'" vAlign=center noWrap><IMG
src="http://xxx.com/url11/images/filesselected.gif" border=0><FONT
color=darkblue>巡检记录</FONT></TD></TR>
<TR>
<TD onmousedown="this.style.border='2 #cccccc inset';"
onmouseover="this.style.border='2 #eeeeee outset';this.style.cursor='hand'"
style="BORDER-RIGHT: #ffffff 2px; BORDER-TOP: #ffffff 2px; BORDER-LEFT: #ffffff 2px; CURSOR: hand; BORDER-BOTTOM: #ffffff 2px"
onclick="getDutyWork('DUTY_WORK_AFFAIR')"
onmouseout="this.style.border='2 #ffffff'" vAlign=center noWrap><IMG
src="http://xxx.com/url11/images/menu_link_default.gif" border=0><FONT
color=darkblue>事务记录</FONT></TD></TR>
.....下面还有
代码里div的id明明是dutymain,为什么getElementById()找不到呢?
改成这个试试document.getElementById("dutymain").getElementById("topbar1").getElementsByTagName("td");
改了,执行到那个地方还是同样的错误提示。
忘了说明xxx.com/url2 页面中<div id=dutymain> </div>之间的代码是加载另外一个xsl文件得到的
如果说页面没有加载完成,我手动点击那个连接是有效的啊
郁闷
如果是这种原因应该如何调整呢?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css"></style>
</head><body>
<div id="t1">
<div id="t2">2</div>
</div>
</body>
</html>
<script type="text/javascript">
var div1 = document.getElementById('t1').getElementsByTagName('div');
alert(div1.length);
alert(div1[0].innerHTML);
var div2 = document.getElementById('t1').getElementById('t2');
alert(div2.innerHTML); //出错,因为getElementById方法是document的,而不是DOM节点都有的。
</script>
除非你自己重新定义了这个方法。否则不要在选择到的元素上再次用这个方法。
改为:
var dutyDOM
if (doc2 && doc2.getElementById("dutymain") && doc2.getElementById("dutymain").getElementById("topbar1") && doc2.getElementById("dutymain").getElementById("topbar1").getElementsByTagName("td")){
dutyDOM = doc2.getElementById("dutymain").getElementById("topbar1").getElementsByTagName("td");} else {
return
}