【问题标题】:VBScript to detect Facebook IE window, press Like and close the windowVBScript检测Facebook IE窗口,按Like并关闭窗口
【发布时间】:2015-06-13 15:56:42
【问题描述】:

我尝试编写一个脚本来检测在 Facebook 上打开的 IE 窗口并点击 Like 按钮。 假设我打开了 10 个 IE。只有其中一个出现在 Facebook 的页面上。 我想让我的脚本检测到那个 IE 窗口,点击这个按钮:

IE.Document.getElementById("pagesHeaderLikeButton")

(上面的按钮是Like按钮)

并关闭该 IE 窗口。

我尝试通过以下方式获取该 IE 窗口:

For Each wnd In CreateObject("Shell.Application").Windows
        If InStr(1, wnd.FullName, "iexplore.exe", vbTextCompare) > 0 Then
           Set IE = wnd
           Exit For
        End If
Next

但这只会将我的 VBscript 设置为第一个打开的 IE,它不会找到 Facebook 窗口。

我试过了:

Dim objInstances, item
Set objInstances = CreateObject("Shell.Application").windows
For Each item In objInstances
    If Item.Name Like "*Internet*" And Item.document.URL Like "*facebook.com*" Then
        IE.Document.getElementById("pagesHeaderLikeButton").Click
    End If
Next

但我得到“未定义子或函数”

【问题讨论】:

    标签: facebook internet-explorer vbscript


    【解决方案1】:

    下一个代码 sn-p 可能会有所帮助:

    Set shApp = CreateObject( "shell.application")
    With shApp
      For Each wnd In .Windows
          If InStr(1, wnd.FullName, "iexplore.exe", vbTextCompare) > 0 Then
            If InStr(1, wnd.document.URL, "facebook.com", vbTextCompare) > 0 Then
              Wscript.Echo "THIS:"
            End If
            Wscript.Echo Left( wnd.document.URL, 70)
          End If
      Next
    End With
    

    输出示例(带有更多 facebook.com 匹配项):

    ==>cscript D:\VB_scripts\SO\30717779a.vbs
    http://www.msn.com/?ocid=iehp
    THIS:
    https://www.facebook.com/login.php?api_key=127760087237610&skip_api_lo
    THIS:
    https://www.facebook.com/literarnifestival1
    THIS:
    https://cs-cz.facebook.com/mgvsetin
    http://www.bing.com/search?q=Xmaster+Official&qs=n&form=QBRE&pq=xmaste
    http://www.bing.com/search?q=%22Xmaster+Official%22&qs=n&form=QBRE&pq=
    
    ==>    
    

    【讨论】:

    • 如果我想点击一个 id="pagesHeaderLikeButton" 的按钮怎么办?
    • shApp.Document.getElementById("pagesHeaderLikeButton").Click 不工作,
    【解决方案2】:

    以下代码将搜索其 URL 中包含 "facebook.com" 的打开 IE 窗口并将它们保存在集合中:

    Dim getIE  As New Collection     
    For Each Item In CreateObject("Shell.Application").Windows
                If Item.Name Like "*Internet*" And Item.document.URL Like "*facebook.com*" Then
                    getIE.Add Item
                End If
    Next
    

    然后你可以循环遍历集合并使用 IE 项目做你想做的事:

    For Each itemIE In getIE
        itemIE.Document.getElementById("pagesHeaderLikeButton").Click ' For example
    Next itemIE
    

    希望这能满足您的要求! ;)

    【讨论】:

    • 我在 Dim ObjShellWindows As New SHDocVw.ShellWindows 处得到一个结束声明
    • 我从:Dim ObjShellWindows As New SHDocVw.ShellWindows修改为Dim ObjShellWindowsSet ObjShellWindows = New SHDocVw.ShellWindows
    • 我还得到了一个未定义的类:SHDocVw
    • 您使用的是哪个版本的 Windows 和 Excel?我编辑了答案,请立即尝试。
    • Windows 8.1 Pro x64 和 Excel 2013。
    【解决方案3】:
    For Each wnd In CreateObject("Shell.Application").Windows
            If InStr(wnd.Name,"Internet") Then
                if InStr(wnd.Document.URL,"facebook.com") Then
                    Set IE2 = wnd
                    Exit For
                End If
            End If
    Next
    

    所以按下按钮会是这样的:

    Set Butlike = IE2.Document.getElementsByTagName("button")
        For Each btn In Butlike
        If btn.type = "submit" Then btn.Click()
    Next
    

    【讨论】:

    • 请使用您问题上的编辑链接添加更多信息。 Post Answer 按钮应仅用于问题的完整答案。
    • 这是问题的完整答案。
    • @XmasterOfficial - 由于“到目前为止”的措辞,这听起来像是一个不完整的答案,或者您的问题的附加信息。如果这是完整的答案,您可能需要稍微改写一下。