创建后访问IE选项卡 [英] Accessing IE tabs once created
问题描述
使用VBA我可以使用以下内容创建一个包含3个不同标签的InternetExplorer对象
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....
- 退出/关闭特定标签?
- 导航到特定标签上的新网址tab?
- 访问特定的标签DOM?
我知道怎么做所有这些使用单个选项卡但不是多个选项卡?
I know how to do all of this with a single tab but not multiple tabs?
推荐答案
我找到了一个似乎有效的答案,尽管我还没有完成严格的测试。我修改了你的代码,这样我就可以在没有你的clsIE模块的情况下运行它。此代码在打开它们时将标签导航到3个URL,然后使用shellwindows对象将它们导航到新的URL。
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.
我更改了do而.busy ..行对于第二个和第三个选项卡不起作用,因为IE applecation的状态已准备好,而新选项卡仍在加载。
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
它使用ShellWindows来操纵标签。这只是通过数字来完成的,所以我猜它可能容易出错。
It uses the ShellWindows to manipulate the tabs. This is only done by number so I'm guessing it could be prone to errors.
为了使它更强大,你可能希望从标签后得到一些信息。操作并在返回时检查值是否相同。例如 sPageTitle = SWs.Item(IETab3Number - 1).Document.Title
可用于在选项卡中存储页面标题,下次要使用时选项卡,您可以检查它是否没有更改。
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.
这篇关于创建后访问IE选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!