VBA代码未从具有类但没有标识

时间:2016-06-16 00:01:49

标签: vba web-scraping

我有一些代码用于打开IE页面并填写第一个下拉列表以显示“caravan”。但是我的代码并没有从下拉列表中选择大篷车。

网站是:

https://insurance.qbe.com.au/portal/caravan/form/estimate

当我右键单击以检查下拉列表的源代码时,我会看到以下内容:

enter image description here

当我在下面展开源代码时,我看到以下内容:

enter image description here

enter image description here

如您所见,与下拉列表相关联的值远低于我最初指向的源代码。

这是我用来尝试选择'大篷车'的代码:

Sub GetQuote()

Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate ("https://insurance.qbe.com.au/portal/caravan/form/estimate")
IE.Visible = True

Do
DoEvents
Loop Until IE.readystate = 4

Dim lists, l
Set lists = IE.document.getElementsByTagName("select")(0)

For Each l In lists

If InStr(1, l.className, "form-control ng-pristine ng-untouched ng-valid", 1) Then
l.SelectedIndex = 0
End If

Next

Application.Wait (Now + TimeValue("00:00:03"))

End Sub

使用上面的代码,我试图通过引用我粘贴在上面的源代码的最后一位来选择'caravan'(即通过获取所有选择标签然后检查标签中的所有项目,直到类名称包含的项目为止找到“form-control ng-pristine ng-untouched ng-valid”,然后选择索引0作为与此项关联的值。这显然不起作用 - 所以这里是我尝试的其他代码,再次没有运气(这是对上面代码的略微修改,只需引用我在检查下拉列表时最初指向的源代码,这将是我粘贴的第一个源代码):

For Each l In lists
If InStr(1, l.className, "ui-select-search ui-select-toggle ng-pristine ng-valid ng-touched", 1) Then
     l.SelectedIndex = 0

1 个答案:

答案 0 :(得分:0)

这是否提供任何帮助,不确定它是否是您的解决方案,但在此处添加了格式化。

Sub test()

Dim ie As SHDocVw.InternetExplorer
Dim htmlDoc As MSHTML.HTMLDocument
Dim inputCollection As MSHTML.IHTMLElementCollection
Dim inputElement As MSHTML.HTMLInputElement

Set ie = New SHDocVw.InternetExplorer

ie.navigate ("https://insurance.qbe.com.au/portal/caravan/form/estimate")

Do While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
    DoEvents
Loop

ie.Visible = 1

Set htmlDoc = ie.document

Set inputCollection = htmlDoc.getElementsByTagName("INput")

For Each inputElement In inputCollection

    Debug.Print inputElement.className

Next inputElement

'或者像这样

Set inputElement = htmlDoc.getElementsByClassName("ui-select-search ui-select-toggle ng-pristine ng-valid ng-touched")(0)


End Sub