Need to automate IE with drop down menu using Excel VBA

空扰寡人 提交于 2019-12-25 03:45:16

问题


I want to automate a site having drop down menu named 'Timesheet' and then click on the Menu item 'Project' which is 3rd in the list.

Here is the HTML code:

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li class="dropdown">
                        <a href="#" ng-click="$event.preventDefault()" title="Timesheet" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="glyphicon glyphicon-time"></span> Timesheet <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a ui-sref="AddTime" title="Add time" href="/Timesheet/AddTime/">Add time</a></li>
                            <li><a ui-sref="ApproveTime" title="Approve time" href="/Timesheet/ApproveTime">Approve time</a></li>
                            <li><a ui-sref="Projects" title="Projects" href="/Timesheet/Admin/Projects">Projects</a></li>
                            <li><a ui-sref="BudgetCode" title="Budget Code" href="/Timesheet/Admin/BudgetCode">Budget Code</a></li>
                            <li><a ui-sref="WorkCode" title="Work Code" href="/Timesheet/Admin/WorkCode">Work Code</a></li>
                            <li><a ui-sref="Functions" title="Functions" href="/Timesheet/Admin/Functions">Functions</a></li>
                            <li><a ui-sref="WorkStreams" title="Work Streams" href="/Timesheet/Admin/WorkStreams">Work Streams</a></li>
                            <li><a ui-sref="EmployeeResource" title="Employees" href="/Timesheet/Admin/EmployeeResource">Employees</a></li>
                        </ul>
                    </li>
                </ul> 
            </div>

VBA code: Private Sub IE_Test() Dim i As Long Dim IE As Object Dim ElementCol As Object

' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")

'IE.Visible = False

IE.Navigate "http://st-toss/"

' Wait while IE loading...
Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
Loop

IE.Visible = True

Set ElementCol = IE.Document.getElementsByClassName("dropdown-menu")

ElementCol.Item(2).Click

' Clean up
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing

Application.StatusBar = ""

End Sub

I have tried selecting by classname/tagname but I have been getting the same error Automation error/Unspecified error once i did not add any break point. And once i put the breakpoint on the get method error message says object invoked disconnected from client.

Please suggest how can i get through this.


回答1:


ElementCol is a collection of elements in the class "dropdown-menu". (You created this collection when you set getElementsByClassName. Notice that "Elements" is plural.)

How many elements are there in that class, and which one is the one you need?
If this is the only element in that class, then you would refer to it by :

elementCol(0).selectedIndex = 2

or something similar.



来源:https://stackoverflow.com/questions/35500393/need-to-automate-ie-with-drop-down-menu-using-excel-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!