简体   繁体   中英

Populate Dropdown list on webpage from an iFrame with VBA

I've got a code which opens a webpage and clicks on a link. This then loads data from an Iframe, which I'm trying to change values on dropdown lists.

I'm trying to populate the Manufr dropdown list from the Year iFrame. using VBA, but I'm getting a Object Variable or With Block Variable not set error. Could this have something to do with the fact that the data I am trying to edit is in an Iframe?

Here is the code I'm trying to use

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

Try:

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

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

EDIT: try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM