通过VBA从网页上的下拉列表中选择一个项目[复制]

问题描述 投票:1回答:1

这个问题在这里已有答案:

我想要一个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
vba web-scraping
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.