在现有Internet Explorer上打开新选项卡(从命令行)

时间:2015-07-15 22:03:55

标签: java internet-explorer browser runtime.exec processbuilder

我想在命令提示符(在Windows中)运行一个命令,该命令将打开具有某些特权的Internet Explorer。

场景(从未打开Internet Explorer开始):

  • 在Internet Explorer上运行“iexplore google.ca”打开google.ca,作为新标签
  • 再次运行“iexplore yahoo.ca”,在最近打开的Internet Explorer上打开yahoo.ca作为新标签

最终结果应该在一个Internet Explorer应用程序/窗口上有2个选项卡。

我想要上述行为,但Internet Explorer只会在运行iexplore命令时打开新窗口而不是新标签。我已经尝试了各种命令行选项和设置。

将使用运行时或ProcessBuilder等Java代码执行此命令。任何其他解决方案也都会很棒。

注意:我找到了一种获取此行为的方法,但它只适用于默认浏览器。我特别想指定Internet Explorer浏览器而不是依赖于默认浏览器。 (开始/ d“”www.google.ca)

1 个答案:

答案 0 :(得分:0)

下一个VBScript可能有所帮助。注释代码中的说明。

' VB Script Document ' http://stackoverflow.com/questions/31441974
' Opens blank page in a new MSIE tab and navigates to the URL if supplied. 
' Usage: ie [URL]
'        if saved as "IE.VBS" somewhere under PATH system environment variable.
'
option explicit
On Error Goto 0
Dim strMyUrl, intWExist, BrowserNavFlag, iRetNav2, objArgs, WshShell, IE, oIE, sTitle

Set objArgs = WScript.Arguments
If objArgs.Count > 0 Then
    strMyUrl = objArgs( 0)
Else
    strMyUrl = "about:blank"
End If

Set WshShell = WScript.CreateObject( "WScript.Shell")
Set IE  = Nothing
Set oIE = Nothing

intWExist = FindIE( strMyUrl, oIE) ' look for MSIE window pointer 

set IE = oIE
iRetNav2 = 0
Select Case intWExist
' Case 3
'   ''' 3 = MSIE window found, URL match, window title match
'   ''' (not implemented yet)
' Case 2
'   ''' 2 = MSIE window found, URL match
Case 1, 2, 3
    ''' 1 = MSIE window found, no URL match
    BrowserNavFlag = navOpenInNewTab ' 2048
    iRetNav2 = IE.Navigate2( strMyUrl, CLng( BrowserNavFlag), "_blank")
Case Else
    ''' 0 = MSIE window not found
    Set IE = CreateObject( "InternetExplorer.Application")
    BrowserNavFlag = 1
    iRetNav2 = IE.Navigate( strMyUrl)
End Select

IE.Visible = True

While IE.Busy
    Wscript.Sleep 100
Wend
While IE.Document.ReadyState <> "complete" '(obsolete?) Or IE.ReadyState <> 4
    Wscript.Sleep 100
Wend

sTitle = ""
intWExist = FindIE( strMyUrl, oIE) ' look for MSIE window title
' AppActivate method could fail (no error) if MSIE window runs minimized
'                               off topic for current question (31441974)
If Not sTitle = "" Then WshShell.AppActivate sTitle

Private Function FindIE( ByVal sUrl, ByRef oObj)
' parameters
' sUrl (input)  string
' oObj (output) object
' returns 
' 0 = any MSIE window not found - or found but not accessible   
' 1 = a MSIE window found
' 2 = 1 and address line match
' 3 = 2 and title match (not implemented yet)
    Dim ww, tpnm, tptitle, tpfulln, tpUrl, tpUrlUnencoded
    Dim errNo, errStr, intLoop, intLoopLimit
    Dim iFound : iFound = 0
    Dim shApp    : Set shApp = CreateObject( "Shell.Application")
    With shApp
        For Each ww In .windows
            tpfulln = ww.FullName
            If Instr( 1, Lcase( tpfulln), "iexplore.exe", vbTextCompare) <> 0 _ 
        and Instr( 1, UCase( tpfulln), "SCODEF:", vbTextCompare) = 0 _ 
        and Instr( 1, UCase( tpfulln), "CREDAT:", vbTextCompare) = 0 Then
                If iFound > 0 Then
                Else
                    Set oObj = ww
                End If
                tptitle = "x x x" : tpUrl = "" : tpUrlUnencoded = ""
                intLoopLimit = 10 ' to look for attributes max. intLoopLimit/10 seconds
                intLoop = 0
                While intLoop < intLoopLimit
                    intLoop = intLoop + 1
                    On Error Resume Next
                    tpnm = typename( ww.document)
                    errNo = Err.Number
                    If errNo <> 0 Then
                        'error if  page not response (yet)' 
                        errStr = "Error # " & CStr( errNo) _
                & " """ & Err.Description & """ 0x" & Hex( errNo)  
                        Wscript.Sleep 100
                    Else
                        iFound = 1
                        intLoopLimit = intLoop  ' end the loop and preserve loop counter
                        tptitle = ww.document.title
                        tpUrl = ww.document.URL
                        tpUrlUnencoded = ww.document.URLUnencoded
                        errStr = tpnm
            sTitle = tptitle
                    End If
                    On Error Goto 0
                Wend
                If Instr( 1, Lcase( tpnm), "htmldocument", vbTextCompare) <> 0 then
                    If Instr( 1, Lcase( tpUrl), Lcase( sUrl), vbTextCompare) <> 0 Then
                        Set oObj = ww
                        iFound = 2
                        ' looking for all matching MSIE URLs 
                        ' this may take considerable time amount
                        ' to speed up script running, uncomment next line "exit for"
                        exit for
                    Else
                    End If 
                End If
            Else
                ' a program reports the same shell.application property as "iexplore.exe"
                ' i.e. "explorer.exe", "HTML preview" in some editors etc.
            End If
        Next
    End With
    Set shApp = Nothing
    FindIE = iFound
End Function
' 
' http://msdn.microsoft.com/en-us/library/aa768360(v=vs.85).aspx
' BrowserNavConstants Enumerated Type
' Contains values used by the IWebBrowser2::Navigate
'                         and IWebBrowser2::Navigate2 methods.
' typedef enum BrowserNavConstants {
Const navOpenInNewWindow      = &h01, _
      navNoHistory            = &h02, _
      navNoReadFromCache      = &h04, _
      navNoWriteToCache       = &h08, _
      navAllowAutosearch      = &h10, _
      navBrowserBar           = &h20, _
      navHyperlink            = &h40, _
      navEnforceRestricted    = &h80, _
      navNewWindowsManaged    = &h0100, _
      navUntrustedForDownload = &h0200, _
      navTrustedForActiveX    = &h0400, _
      navOpenInNewTab         = &h0800, _
      navOpenInBackgroundTab  = &h1000, _
      navKeepWordWheelText    = &h2000, _
      navVirtualTab           = &h4000, _
      navBlockRedirectsXDomain= &h8000, _
      navOpenNewForegroundTab = &h010000
' } BrowserNavConstants;
'