VBScript:无法自动找到/点击网页上的按钮

时间:2017-07-25 15:39:02

标签: button vbscript onclick

请在这里使用一些帮助。我们必须维护一堆笔记本电脑,这些笔记本电脑绝不会连接到互联网(出于安全考虑)。有一台相同类型的笔记本电脑和磁盘映像连接到互联网进行补丁管理。

他们运行Windows 10,在互联网笔记本电脑上,我使用的脚本使用wsus2 cab文件查找缺失的更新。

现在我想从命令行获取该脚本的输出,并让它为我下载缺少的更新。

在我的脚本中,我导入调查结果,找到KB编号,将该KB编号附加到“https://www.catalog.update.microsoft.com/Search.aspx?q=”的末尾,获取变量中的网页,解析变量以获取补丁名称,然后尝试以获取与修补程序名称对应的下载按钮。

例如,输出显示“2017-07版基于x64的系统的Adobe Flash Player for Windows 10版本1703安全更新(KB4025376)”缺失。

所以我得到了URL,并下载了源代码......看起来像这样:

<td class="resultsbottomBorder resultspadding" id="9ebe5d13-4966-4cdb-a612-9780df621411_C1_R0">
    <a id='9ebe5d13-4966-4cdb-a612-9780df621411_link' href="javascript:void(0);" onclick='goToDetails("9ebe5d13-4966-4cdb-a612-9780df621411");'>
        2017-07 Security Update for Adobe Flash Player for Windows 10 Version 1703 for x64-based Systems (KB4025376)
    </a>
</td>
<td class="resultsbottomBorder resultspadding" id="9ebe5d13-4966-4cdb-a612-9780df621411_C2_R0">
    Windows 10
</td>
<td class="resultsbottomBorder resultspadding" id="9ebe5d13-4966-4cdb-a612-9780df621411_C3_R0">
   Security Updates
</td>
<td class="resultsbottomBorder resultspadding " id="9ebe5d13-4966-4cdb-a612-9780df621411_C4_R0">
    7/11/2017
</td>
<td class="resultsbottomBorder resultspadding" id="9ebe5d13-4966-4cdb-a612-9780df621411_C5_R0">
    n/a
</td>
<td class="resultsbottomBorder resultspadding resultsSizeWidth" id="9ebe5d13-4966-4cdb-a612-9780df621411_C6_R0">
    <span id="9ebe5d13-4966-4cdb-a612-9780df621411_size">20.7 MB</span> 
    <span class="noDisplay" id="9ebe5d13-4966-4cdb-a612-9780df621411_originalSize">21711921</span>
</td>
<td class="resultsbottomBorder resultsButtonWidth" id="9ebe5d13-4966-4cdb-a612-9780df621411_C7_R0">
    <input id="9ebe5d13-4966-4cdb-a612-9780df621411" class="flatLightBlueButton" type="button" value='Download' />
</td>

我说“尝试”,因为这是脚本不起作用的地方。

在发帖之前,我查看了Click Button on Webpage via VBScript?。没有帮助。我发布这个,所以你们都不认为我事先没有RTFM。

脚本失败了(是的,我现在正在硬编码ID只是为了让它正常运行......以后会变成一个变量):

IE.Document.getElementsByID("9ebe5d13-4966-4cdb-a612-9780df621411").Item(0).Click

我也试过使用http提供程序,而不是IE对象,但也失败了:

http.Document.getElementByID("9ebe5d13-4966-4cdb-a612-9780df621411").Item(0).Click

这是我的剧本:

' Test data -- not going to be in the production script.  The output from the wsus scan 
' will be imported here.

Dim findings(1)

findings(0) = "2017-07 Cumulative Update for Windows 10 Version 1703 for x64-based Systems (KB4025342_Fail)"
findings(1) = "2017-07 Security Update for Adobe Flash Player for Windows 10 Version 1703 for x64-based Systems (KB4025376)"

' *****
' Production script follows
' *****

' An array to hold all the findings from the wsus output.  
' This array will change in size and is not declared as fixed.
Dim patches()
' Just a counter.
Dim i : i = 0

' Ints from parsing the strings created fromn the findings.
Dim firstParen, lastParen, length

Dim kb
Dim kbAndNameHash : Set kbAndNameHash = CreateObject("Scripting.Dictionary")

' Iterate through the results and populate the array... putting this stuff into
' an array because the original MS script just redirected the findings to 
' output -- you couldn't do anything automatically with it afterwards.
For Each item In findings 
    ReDim Preserve patches(i)
    patches(i) = item 
    i = i + 1
Next 

For Each thingy In patches
    ' Need to parse the string to find just the KB number.  The "(KB...)" appears to be
    ' unique.
    firstParen = InStr(thingy, "(KB") 
    lastParen  = InStr(thingy, ")")
    ' The -1 in this line is to exclude the closing paren in (KB...) -- we just want digits
    ' no special characters.
    length = ((lastParen - 1) - firstParen)

    ' Just some debug
    ' WScript.Echo "firstParen: " & firstParen & vbCR & _
    '              "lastParen:  " & lastParen  & vbCR & _
    '              "length:     " & length     & vbCR


    ' Get the KB number by reading the string from the next spot after the first paren to 
    ' the length of the number (because it's not always a fixed length).
     kb = Mid(thingy, firstParen + 1, length)

    ' Create a hash for the kb and name.  A hash is easier to work with
    ' than a 2 dimensional array.  kb is needed for the patch URL.  The name
    ' is needed for what to download once the page is loaded.
     kbAndNameHash.Add kb, thingy
Next

' Provides access to the scripting dictionary
Dim k             : k = kbAndNameHash.Keys
Dim n             : n = kbAndNameHash.Items

' Get the patches
Dim http       : Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
Dim partialURL : partialURL = "https://www.catalog.update.microsoft.com/Search.aspx?q="
Dim webpage, j 

' #####
' Tried from: https://stackoverflow.com/questions/5292860/click-button-on-webpage-via-vbscript

Dim objWshShell,IE,searchStr

Set objWshShell = Wscript.CreateObject("Wscript.Shell")
Set IE = CreateObject("InternetExplorer.Application")

' #####

For Each key In k

    With http
        .Open "GET", partialURL & key, True
        .Send 
        .WaitForResponse
    End With

    ' Load the webpage source into memory.
    webpage = http.ResponseText
    ' Find the postion of the name of the finding.
    j = InStr(webpage, kbAndNameHash.Item(key))

    If http.Status = 200 Then

        If j > 0 Then

            ' This is where the script no longer works.
            ' I also tried this:
            ' http.Document.getElementByID("9ebe5d13-4966-4cdb-a612-9780df621411").Item(0).Click

            ' #####
            ' Tried from: https://stackoverflow.com/questions/5292860/click-button-on-webpage-via-vbscript

            With IE
                .Visible = True
                .Navigate partialURL & key

                Do While .Busy
                WScript.Sleep 100
            Loop

            .Document.getElementsByID("9ebe5d13-4966-4cdb-a612-9780df621411").Item(0).Click
        End With

        ' #####

        Else
        MsgBox "Did not find a download link for: " & vbCR & vbCR &_
                    kbAndNameHash.Item(key) & vbCR & vbCR &_
                    "Please manually check for " & key, 48, "Patch not found"
        End If

    Else

       ' TODO: handle the <>200 status

    End If

Next

Set http          = NOTHING
Set kbAndNameHash = NOTHING
WScript.Quit

如何获得有关如何获得与指定补丁名称相对应的“下载”按钮的任何帮助都将非常感谢!!!

谢谢!

0 个答案:

没有答案
相关问题