Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

internet explorer - Accessing IE tabs once created

Using VBA I can create an InternetExplorer object with 3 different tabs using the following

Option Explicit

Public Enum IE_READYSTATE
    Uninitialised = 0
    Loading = 1
    Loaded = 2
    Interactive = 3
    complete = 4
End Enum

Sub Example_Click()
Dim ieApp As clsIE

    'Create IE and login
    If ieApp Is Nothing Then
        Set ieApp = New clsIE
        With ieApp

            'IE Tab1
            .IE.Visible = True
            .IE.navigate "http://www.bbc.co.uk/news/"
            Do While .IE.Busy Or Not .IE.readyState = IE_READYSTATE.complete: DoEvents: Loop

            'IE Tab2
            .IE.Navigate2 "http://www.bbc.co.uk", CLng(2048)
            Do While .IE.Busy Or Not .IE.readyState = IE_READYSTATE.complete: DoEvents: Loop

            'IE Tab3
            .IE.Navigate2 "http://www.bbc.co.uk", CLng(2048)
            Do While .IE.Busy Or Not .IE.readyState = IE_READYSTATE.complete: DoEvents: Loop


        End With
    End If

End Sub

How can I then access these tabs to....

  1. Quit/Close a specific tab?
  2. Navigate to a new URL on a specific tab?
  3. Access a specific tab DOM?

I know how to do all of this with a single tab but not multiple tabs?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have found an answer that appears to work, though I haven't done rigorous testing. I've modified your code so that I can run it without your clsIE module. This code navigates the tabs to 3 urls when opening them and then navigates them to new urls using the shellwindows object.

I changed the do while .busy.. lines as they didn't work for the 2nd and 3rd tabs in that the state of the IE applecation was ready whilst the new tabs were still loading.

Sub Example_Click()
Dim ieApp As InternetExplorer
Dim SWs As ShellWindows
Dim IETab1Number As Integer
Dim IETab2Number  As Integer
Dim IETab3Number As Integer

Set SWs = New ShellWindows

'Create IE and login
If ieApp Is Nothing Then
    Set ieApp = CreateObject("InternetExplorer.Application")
    With ieApp

       'IE Tab1
       .Visible = True
       .Navigate "http://www.bbc.co.uk/news/"
       Do While .Busy Or Not .ReadyState = IE_READYSTATE.complete: DoEvents: Loop
       IETab1Number = SWs.Count

       'IE Tab2
       .Navigate2 "http://www.bbc.co.uk/news/uk-scotland-north-east-orkney-shetland-23822420", CLng(2048)
       'Do While .Busy Or Not .ReadyState = IE_READYSTATE.complete: DoEvents: Loop
       Do While SWs.Count = IETab1Number: DoEvents: Loop

       IETab2Number = SWs.Count

       'IE Tab3
       .Navigate2 "http://www.bbc.co.uk", CLng(2048)
       'Do While .Busy Or Not .ReadyState = IE_READYSTATE.complete: DoEvents: Loop
       Do While SWs.Count = IETab2Number: DoEvents: Loop
       IETab3Number = SWs.Count
       'ieApp.Visible = False

       SWs.Item(IETab1Number - 1).Navigate "http://www.bbc.co.uk/"
       SWs.Item(IETab2Number - 1).Navigate2 "http://www.bbc.co.uk/news/"
       SWs.Item(IETab3Number - 1).Navigate2 "http://www.bbc.co.uk/news/"


    End With
End If
Set ieApp = Nothing
Set SWs = Nothing

End Sub

It uses the ShellWindows to manipulate the tabs. This is only done by number so I'm guessing it could be prone to errors.

To make it more robust you may want to get some info from the tab after an operation and check that the value is the same when returning to it. For example sPageTitle = SWs.Item(IETab3Number - 1).Document.Title could be used to store the title of the page in a tab, the next time you want to use the tab you can check that it hasn't changed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...