VBA尝试在网页IE中导航



我正在尝试自动化一个报告。该网站没有不同选项卡的特定URL。我一直遇到错误,我尝试过使用getelementsby ID和Classname,但它不起作用。这是我第一次使用VBA和IE。

遇到的错误为462远程服务器计算机不存在或不可用找不到元素ID 时出错

Sub openidp()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")

With IE
.navigate "http://idp/analytics/saw.dll?dashboard&PortalPath=%2Fshared%2F3.0%20Tenancy%20Management%2F_portal%2F3.0.4%20Area%20Manager%20Tenancy%20Toolkit"   
'navigate to page
Do Until IE.readyState = READYSTATE_COMPLETE  'Wait until page finished loading
DoEvents
Loop  
End With
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 15
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime  'allow time for username and password to be 
'added and for page to open up
Set element = IE.Document.getElementsByID("dashboard_page_10_tab").Click
Do Until IE.readyState = READYSTATE_COMPLETE
DoEvents
Loop
End Sub

来自inspect元素的Html脚本(不确定如何使其看起来整洁(

table class="masterH3 masterTabBarTabSecondaryEnabled secondaryTabEnabled 
tabContainer" 
id="dashboard_page_10_tab" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td title="Monitors trends in the level of rent debt broken down
by week or month">
<div tabindex="0" title="Monitors trends in the level of 
rent debt broken down by week or month" 
style="margin-right: 1px; margin-left:1px;">
Debt
</div>
</td>
</tr>
</tbody>
</table

问题是Internet Explorer与VBA断开连接(在我的情况下是一些内部安全设置(。解决方案是将Internet Explorer重新连接到VBA:

在IE不再响应并且现有的Internet Explorer窗口将被分配给=IEObject1 之后包含此代码

Dim shellWins As SHDocVw.ShellWindows
Dim explorer As SHDocVw.InternetExplorer
Set shellWins = New SHDocVw.ShellWindows
For Each explorer In shellWins
If explorer.Name = "Internet Explorer" Then
Set IEObject1 = explorer
Debug.Print explorer.LocationURL
Debug.Print explorer.LocationName
End If
Next
Set shellWins = Nothing
Set explorer = Nothing

如果你打开了几个IE窗口,那么这段代码会选择最后一个。如果需要打开多个窗口,可以使用URL或LocationName在窗口之间进行选择。

最新更新