VBA - IE自动化 - .getelements

时间:2017-12-05 04:18:32

标签: excel vba internet-explorer automation

我试图将文本输入到没有ID的输入字段中。当我检查元素时,我得到了这个:

<input title="Affected Customer(s)" class="custom-input ng-pristine ng-invalid ng-invalid-required ng-touched" role="combobox" aria-expanded="false" aria-required="true" aria-owns="typeahead-78-1606" aria-autocomplete="list" required="" type="text" placeholder="Enter customer name, email, login ID or corporate ID" entity="incident" field-name="customer.loginId" potentially-required-field="" auto-focus="" typeahead="user as user.firstName + ' ' + user.lastName for user in getList('person', $viewValue)" typeahead-loading="person.isLoading" typeahead-wait-ms="500" typeahead-on-select="onCustomerSelect($model)" typeahead-template-url="views/create/custom-add-user-dropdown.html" typeahead-min-length="3" prevent-click-event="" ng-model="person.ngModel">

如何引用此输入字段?我尝试了很多方法,但都失败了。我认为我的理解是缺乏的,希望一些帮助能让我走上正轨。我现在尝试找到此输入字段的代码是:

Sub Automate_IE_Load_Page()
'This will load a webpage in IE
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element

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

'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True

'Define URL
URL = "https://ewpg-app-1041.hydro.mb.ca/ux/smart-it/#/create/incident"

'Navigate to URL
IE.navigate URL



' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.readyState = 4: DoEvents: Loop   'Do While
Do Until IE.readyState = 4: DoEvents: Loop   'Do Until


objIE.Document.getElementsByName("customer.loginId")(0).Value = "test"

'click create new
'for each <a> element in the collection of objects with class of 'result__a'...
For Each aEle In objIE.Document.getElementsByName("Affected Customer(s)")

    '...get the href link and print it to the sheet in col C, row y
    result = aEle

'repeat times the # of ele's we have in the collection
Next


'Unload IE
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing

End Sub

行&#34; objIE.Document.getElementsByName(&#34; customer.loginId&#34;)(0).Value =&#34; test&#34;&#34;给我错误运行时错误424 - 预期的对象。

感谢您提供任何帮助,这可能非常简单,但我只是不明白基本的理解。上面的代码取自各个网站,似乎工作正常。我看起来正在使用不同的逻辑进行测试。

1 个答案:

答案 0 :(得分:0)

请尝试使用以下代码:

getElementsByClassName("custom-input ng-pristine ng-invalid ng-invalid-required ng-touched")