首页 文章

创建VBScript脚本检测iexplore

提问于
浏览
1

我想创建一个VBScript脚本来检测Internet Explorer打开页面并在同一窗口中打开新选项卡 . 例如,当我在www.google.com上手动打开Internet Explorer时,VBScript将执行以下操作:

  • 使用www.example.com在同一窗口中检测并打开新标签页

  • 但只做一次

我尝试使用此代码:

Set wshShell = CreateObject("WScript.Shell")
Do
    page1 = wshShell.AppActivate("Blank page - Internet Explorer")
    If page1 = True Then
        Set page2 = CreateObject("InternetExplorer.Application")
        page2.Navigate "http://www.example.com", CLng(navOpenInNewTab)
    End If
    WScript.Sleep 500
Loop

1 回答

  • 0

    首先,您必须使用现有的Internet Explorer实例,而不是创建新的实例 . 不幸的是,人们可能期望能够做到这一点( GetObject(, "InternetExplorer.Application") )对Internet Explorer COM对象不起作用 . AppActivate 也没有返回应用程序对象的句柄,您需要调用它的方法 . 相反,你需要这样做:

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

    如果要选择打开特定页面的实例,可以检查该页面的 Headers 以进行选择:

    wnd.Document.Title = "something"
    

    要么

    InStr(1, wnd.Document.Title, "something", vbTextCompare) > 0
    

    你的第二个问题是VBScript无法识别符号常量 navOpenInNewTab . 您必须使用数值(在BrowserNavConstants枚举中定义):

    ie.Navigate "http://www.example.com", CLng(2048)
    

    或者先自己定义常量:

    Const navOpenInNewTab = &h0800&
    ie.Navigate "http://www.example.com", navOpenInNewTab
    

    请注意,此处必须使用十六进制表示法和尾随符号,因为该值必须为Long,并且必须在 Const 定义中使用文字 . 调用函数 CLng 这样的表达式无效 .


    或者,您可以通过完全省略Navigate方法的第二个参数来打开新选项卡中的URL,而是为第三个参数提供值 "_blank"

    ie.Navigate "http://www.example.com", , "_blank"
    

    但是,我不确定这会在当前窗口中打开一个新选项卡(可能取决于浏览器的选项卡设置),所以我建议使用上面描述的第二个参数(flags) .

相关问题