0

我正在努力在网页上设置一些非常尴尬的字段(我无法控制的字段)的值,请重视研究人员的输入。我制作了一个网页模型,展示了这个问题。开始:

<html>
<body>

<div id="mydiv1" style="position: none; display: block; z-index: 1; width: 800px;">
<p>First div (mydiv1) starts here.</p>
<br />
<div id="mydiv2" style="position: none; display: block; z-index: 6; width: 800px;">
<p>Second div (mydiv2) starts here. The following field in blue is arranged thusly: <i>div>div>table>form>table</i>; and its tabIndex is 1.</p>

<table border="0" cellspacing="1" cellpadding="0" width="410">
<tbody>
<tr>
<td colspan="3" style="width: 410px; background-color: #5698eb;">

<form id="entryform" name="entryform">

<table border="0" cellspacing="1" cellpadding="0">
<tbody>
<tr>
<td width="100" valign="center" class=text1">
<br /><b>Project: </b>
</td>
<td width="5"></td>
<td valign="centre">
<input class="formclass_input1" type="text" tabIndex="1" name="entryform_field">
</td>
</tr>
</tbody>
</table>
</form>
</td>
</tr>
</tbody>
</table>

<br />
<div id="mydiv3" style="position: none; display: block; z-index: 16; width: 850px;">
<p>Third div (mydiv3) starts here. The following fields are arranged thusly: <i>div>div>div>form>table</i> and the tabIndexes go 1,2,3,4.<br />The field names change randomly from record to record, but their tabIndexes are always the same.</p>

<form id="myform" name="myform">

<table border="0" cellspacing="1" cellpadding="0">
<tbody>
<tr>
<td width="100" valign="center" class=text1">
<b>Name: </b>
</td>
<td width="5"></td>
<td valign="centre">
<input class="formclass_input1" type="text" tabIndex="1" name="changeable_field_name1">
</td>
</tr>
</tbody>
</table>

<table border="0" cellspacing="1" cellpadding="0">
<tbody>
<tr>
<td width="100" valign="center" class=text1">
<b>Date: </b>
</td>
<td width="5"></td>
<td valign="centre">
<input class="formclass_input1" type="text" tabIndex="2" name="changeable_field_name2">
</td>
</tr>
</tbody>
</table>

<table border="0" cellspacing="1" cellpadding="0">
<tbody>
<tr>
<td width="100" valign="center" class=text1">
<b>Company: </b>
</td>
<td width="5"></td>
<td valign="centre">
<input class="formclass_input1" type="text" tabIndex="3" name="changeable_field_name3">
</td>
</tr>
</tbody>
</table>

<table border="0" cellspacing="1" cellpadding="0">
<tbody>
<tr>
<td width="100" valign="center" class=text1">
<b>Address: </b>
</td>
<td width="5"></td>
<td valign="centre">
<input class="formclass_input1" type="text" tabIndex="4" name="changeable_field_name4">
</td>
</tr>
</tbody>
</table>
</form>
</div></div></div>

</body>
</html>

这里的关键是字段名称随机更改。我以前在堆栈上提出了我的问题,但我把问题简单化了!一个叫 Panayot 的人帮我完美地解决了这个问题,但是当我将修改后的脚本介绍到现场时,它失败了。因此,使这个更准确的模型。它比我第一次介绍的要复杂。使用 Panayot 的代码,我的脚本目前如下所示:

surl ="http://[website.com]"
set ie = nothing
set shapp=createobject("shell.application")
on error resume next
For Each owin In shapp.Windows
     if left(owin.document.location.href,len(surl))=surl then
        if err.number = 0 then
        set ie = owin
          end if
       end if
err.clear
Next
on error goto 0
if ie is nothing then
    wscript.echo "Window Not Open"
else

Set myDiv = IE.Document.All.mydiv3

If Not myDiv Is Nothing Then
    Set nodes = myDiv.childNodes
    For i = 0 To nodes.Length-1 Step 2
        If nodes(i).tabIndex = "1" Then
            nodes(i).Value = "ta-da!"
            'nodes(i).Value = nodes(i).tabIndex
            Exit For
        End If
    Next
End If

End If

我已经用它做了很多实验,但我只是以我有限的理解无法弄清楚。基本上最后的 IF 语句没有让我进入 tabIndex "1"。如果这是可以解决的,我会是一个非常快乐的人。任何输入表示赞赏。谢谢你。

4

2 回答 2

2

很高兴看到我的代码很有帮助。需要注意的是,.childNodes属性返回元素的直接子元素。您要查找的元素嵌套更深。我将使用空格格式复制您的示例代码,以更清楚地查看您的实际层次结构。

<div id="mydiv1">
  <div id="mydiv2">
    <table>
      <tbody>
        <tr>
          <td>
            <form id="entryform" name="entryform">
              <table>
                <tbody>
                  <tr>
                    <td></td>
                    <td></td>
                    <td valign="centre">
                      <input tabIndex="1" name="entryform_field">
                    </td>
                  </tr>
                </tbody>
              </table>
            </form>
          </td>
        </tr>
      </tbody>
    </table>
  </div> <!-- end of mydiv2 -->
</div>  <!-- end of mydiv1 -->

我没有看到DIV带有ID“mydiv3”的a,也许是错字?无论如何,您可以结合.childNodes.firstChild属性来浏览层次结构树,但这将是痛苦的编码。您可以使用功能简化该过程getElementsByTagName

Set elm1 = IE.Document.All.mydiv1
MsgBox "Tables " & elm1.getElementsByTagName("table").Length '2
MsgBox "Forms  " & elm1.getElementsByTagName("form" ).Length '1
MsgBox "Inputs " & elm1.getElementsByTagName("input").Length '1

这样,您可以从更接近实际元素的级别开始搜索。

Set myInput = Nothing
For Each elm In IE.Document.All.mydiv1.getElementsByTagName("input")
    If elm.tabIndex = "1" Then
        Set myInput = elm
        Exit For
    End If
Next

If Not myInput Is Nothing Then
    myInput.Value = "ta-da!"
End If
于 2013-04-11T20:13:49.787 回答
0

用“on error resume next”注释掉该行并查看存在哪些错误(如果有)。

于 2013-04-11T16:27:37.280 回答