VBS: target fields by div and tabIndex from external .vbs

两盒软妹~` 提交于 2019-12-11 18:09:21

问题


I am struggling to set the values of some very awkward fields on a webpage (one that I do not control), would value a boffin's input, please. I have made a mock up of the webpage, demonstrating the issue. Here goes:

<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>

The critical thing here is the field names change, randomly. I have presented my problem previosuly on stack, but I oversimplified the problem! A fellow named Panayot helped and solved it for me perfectly, but when I introduced my revised script to the live site, it failed. Hence making this more accurate mock-up. It is more complicated than I first presented. Using Panayot's code, my script currently looks like this:

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

I've experimented a lot with it, but I just can't figure it out with my limited understanding. Basically the final IF statement is not getting me into the tabIndex "1". If this is solvable I'll be a very happy guy. Any input is appreciated. Thank you.


回答1:


Glad to see my code was helpful. Need to note that .childNodes property return immediate children of an element. The element you looking for is more deeply nested. I'll copy your example code with space formating to see more clearly your actual hierarchy.

<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 -->

I not see a DIV with ID "mydiv3", maybe it's a typo? Anyway, you can combine .childNodes and .firstChild properties to browse into hierarchy tree, but that w'd be painful coding. You can simplify that process with getElementsByTagName function.

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

This way you can start searching from a level more closer to the actual element.

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



回答2:


Comment out the line with 'on error resume next' and see what, if any, errors are present.



来源:https://stackoverflow.com/questions/15945820/vbs-target-fields-by-div-and-tabindex-from-external-vbs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!