使用Excel VBA单击悬停下拉菜单

时间:2016-09-20 04:33:24

标签: javascript excel vba web-scraping

我们如何使用VBA选择下拉选项?

例如,我的HTML源代码如下所示,我尝试选择第二个,“코스피200선물”。

<select name="isu_cd" id="isu_cdc74d97b01eae257e44aa9d5bade97baf">
    <option value="ALL">전체</option>
    <option value="KRDRVFUK2I">코스피200 선물</option>
    ....
</select>

因此,在选择name =“isu_cd”之后,我尝试使用“For Each”迭代它们。

但是“For Each”中对“MsgBox(botton.innnerHTML)”的测试给了我很多HTML代码,其他所有选项都是意外的。

Unexpected Results

我们可以用我的方法迭代这个吗?

Sub test_javascript()

    url = "http://marketdata.krx.co.kr/mdi#document=060101"

    Set ie = CreateObject("InternetExplorer.Application")
        ie.Visible = True
        ie.navigate url

    Do Until (ie.readyState = 4 And Not ie.Busy)
        DoEvents
    Loop

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

    Set Buttons = ie.document.getElementsByName("isu_cd")

    For Each botton In Buttons

        MsgBox (botton.innerHTML) 'it gives all of HTML code unexpectedly

        If botton.innerHTML = "코스피200 선물" Then 'Nothing comes to here
            MsgBox (botton.innerHTML)
            botton.FireEvent ("onchange")
            Exit For
        End If
    Next

End Sub

1 个答案:

答案 0 :(得分:0)

问题解决了!

按钮(0).Value =“KRDRVFUK2I”将下拉框更改为我想要的内容。

完整的解决方案如下。

Sub test_javascript()

    url = "http://marketdata.krx.co.kr/mdi#document=060101"

    Set ie = CreateObject("InternetExplorer.Application")
        ie.Visible = True
        ie.navigate url

    Do Until (ie.readyState = 4 And Not ie.Busy)
        DoEvents
    Loop

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

    Set Buttons = ie.document.getElementsByName("isu_cd")

    Buttons(0).Value = "KRDRVFUK2I"

    Set goandfind = ie.document.getElementsByClassName("btn-board btn-board-search")(0)
    goandfind.Click

    Do Until (ie.readyState = 4 And Not ie.Busy)
        DoEvents
    Loop

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

    Set buttons_excel = ie.document.getElementsByTagName("button")

    For Each botton In buttons_excel
        If botton.innerHTML = "Excel" Then

            MsgBox (botton.outerHTML)

            botton.FireEvent ("onclick")
            Exit For

        End If
    Next

    ' do whatever

End Sub