使用VBA从iFrame填充网页上的下拉列表

时间:2016-03-08 05:26:44

标签: javascript html excel vba excel-vba

我有一个代码可以打开网页并点击链接。然后,这会从Iframe加载数据,我试图更改下拉列表中的值。

我尝试填充Manufr iFrame中的Year下拉列表。使用VBA,但我收到Object Variable or With Block Variable not set错误。这可能与我试图编辑的数据在iframe中的事实有关吗?

以下是我尝试使用

的代码
Sub Scrape2()
    Dim Browser As InternetExplorer
    Dim Doc As HTMLDocument
    Dim element As IHTMLElement

    Set Browser = New InternetExplorer
    Browser.Visible = True
    Browser.navigate "http://catalog.xxxxxxx.com"

    Do While Browser.Busy And Not Browser.readyState = READYSTATE_COMPLETE
        DoEvents
    Loop

        For Each l In Browser.document.getElementsByTagName("a")
        If l = "http://catalog.xxxxx.com/Catalog.asp?VehicleRef=2" Then
            l.Click
            Exit For
        End If
        Next



    Stop

    Set Doc = Browser.document

        Dim mainIframe
        Dim subIframe
        Set mainIframe = Doc.frames.Item(1).document
        Set subIframe = mainIframe.frames.Item(0).document
        Set element = mainIframe.getElementById("Manufacturer").selectedIndex = 1
        element.FireEvent ("onchange")

    Set Doc = Nothing
    Set Browser = Nothing
End Sub

1 个答案:

答案 0 :(得分:1)

尝试:

Dim d2
Set d2 = Doc.frames("Year").document

Set element = d2.getElementById("Manufacturer").selectedIndex = 1
element.FireEvent ("onchange")

编辑:试试这个

Dim d2, evt
Set d2 = Doc.frames("main").document.frames("year").document
Set element = d2.getElementById("Manufacturer")

If Not element Is nothing Then

    Debug.Print "Got drop-down"
    element.selectedIndex = 1

    'either this...
    element.FireEvent ("onchange")

    'or try this...
    Set evt = d2.createEvent("HTMLEvents")
    evt.initEvent "change", True, False
    element.dispatchEvent evt

Else
    Debug.Print "couldn't get drop-down!"
End if