通过VBA从网页的下拉列表中选择一个项目

时间:2017-12-16 07:00:25

标签: vba web-scraping

我想要一个VBA代码在XE货币转换器网站上转换货币并给我结果。我没有得到如何从列表中选择货币。下面是我的代码:

Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer

ie.Visible = True
ie.navigate "http://www.xe.com/currencyconverter/"
Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "loading web page...."
DoEvents
Loop
Set html = ie.document
Dim drp As HTMLFormElement
html.getElementById("amount").Value = 100
'html.getElementById("from").Value = "Euro"
Set drp = html.getElementById("from_sc")
drp.Click

1 个答案:

答案 0 :(得分:2)

如果我错过了什么,请告诉我。根据需要更改包含GBP - British PoundEUR - Euro的字段。

Sub Get_Result()
    Dim IE As New InternetExplorer, html As HTMLDocument
    Dim post As Object, elem As Object

    With IE
        .Visible = True
        .navigate "http://www.xe.com/currencyconverter/"
        Do Until .readyState = READYSTATE_COMPLETE: Loop
        Set html = .document
    End With

    html.getElementById("amount").Value = 100
    html.getElementById("from_sc").Click

    For Each post In html.getElementsByClassName("ac_odd ac_recent")
        If InStr(post.innerText, "EUR - Euro") > 0 Then post.Click: Exit For
    Next post

    html.getElementById("to_sc").Click

    For Each elem In html.getElementById("to_scroller").getElementsByClassName("ac_even ac_recent")
        If InStr(elem.innerText, "GBP - British Pound") > 0 Then elem.Click: Exit For
    Next elem

    html.getElementById("ucc_go_btn_svg").Click
End Sub