簡體   English   中英

無法通過 VBA 在 Internet Explorer 中選擇下拉列表

[英]Unable to select dropdown list in internet explorer through VBA

我正在嘗試從本網站的列表中選擇貨幣: https : //www1.oanda.com/currency/converter/

問題是值被輸入到這些字段中,選擇基本上是貨幣,但當我們手動輸入時它會刷新。 通過宏,值被輸入,但 javascript 或任何背景場景轉換值不會發生。 我不能使用任何其他網站進行貨幣轉換。 任何幫助將不勝感激。

貨幣值(在 excel 工作表中)導出到 curr1,curr2 變量

這是代碼

'Option Explicit
Sub converter()

Dim ie As Object
Dim doc As HTMLDocument
Dim inputval, returnval As String
Dim starttime As Double
starttime = Timer
Dim Curr1, Curr2 As String
Dim i As Integer
Dim mywb As Workbook
Dim myws As Worksheet
Set mywb = ThisWorkbook
Set myws = mywb.Worksheets("Ui")

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

'Navigating to the URL
ie.navigate "https://www1.oanda.com/currency/converter/"

'Letting the browser fully load
Do While ie.Busy Or ie.readyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop


Set doc = ie.document
Range(Cells(4, 9), Cells(Rows.Count, 9)).ClearContents

Do While myws.Cells(4 + i, 4).Value <> ""

Curr1 = myws.Cells(4 + i, 5).Value
Curr2 = myws.Cells(4 + i, 7).Value
inputval = myws.Cells(4 + i, 8).Value


'ENTERRING CURRENCY VALUES
doc.getElementById("quote_currency_input").Value = Curr1
doc.getElementById("base_currency_input").Item.innerText = Curr2

'ENTERING VALUE TO BE CONVERTED
returnval = doc.getElementById("base_amount_input").Value


'Do While IE.Busy Or IE.readyState <> 4
Application.Wait (Now + TimeValue("0:00:05"))
'Loop


myws.Activate
myws.Cells(4 + i, 9).Value = returnval


i = i + 1
Loop

'IE.Quit
'MsgBox "Currencies have been converted" & vbNewLine & "Time Taken - " & Format((Timer - starttime) / 86400, "hh:mm:ss")
End Sub

多么挑戰啊! 我當然不是經驗最少的人,但也肯定不是最好的。

當我閱讀您的文字時,我已經清楚這些是 HTML 事件。 我已經知道該網站,但我不知道會發生什么。 我現在投入了幾個小時,但最終我破解了它。

以下帶有附加 Sub() 的宏可以解決您的問題。 有關更多信息,請參閱宏中的注釋。 解決方案對我來說非常困難,但我沒有學到任何東西,因為所有知識都在那里。 但不是這樣。

最后一切似乎都很簡單。 你不會相信我嘗試了多少事件組合。

拿這個宏來說,它有效:

Sub OandaCurrencyConverter()

Dim ie As Object
Dim doc As Object
Dim nodeCurrencyDropdown As Object
Dim nodeAllCurrencies As Object
Dim nodeOneCurrency As Object
Dim starttime As Double
Dim Curr As String
Dim row As Long
Dim i As Byte
Dim leftRightIdentifier As String
Dim myws As Worksheet

  starttime = Timer
  Set myws = ThisWorkbook.Worksheets("Ui")
  myws.Range(myws.Cells(4, 9), myws.Cells(myws.Rows.Count, 9)).ClearContents 'Delete previous results

  'Initialize Internet Explorer, set visibility,
  'call URL and wait until page is fully loaded
  Set ie = CreateObject("internetexplorer.application")
  ie.Visible = False
  ie.navigate "https://www1.oanda.com/currency/converter/"
  Do Until ie.readyState = 4: DoEvents: Loop
  Set doc = ie.document

  'Get results
  Do While myws.Cells(4 + row, 4).Value <> ""
    'ENTERING VALUE TO BE CONVERTED
    'If this value is entered first, the desired result is calculated
    'automatically when the currencies are set in the dropdowns
    doc.getElementById("quote_amount_input").Value = myws.Cells(4 + row, 8).Value

    'ENTERRING CURRENCIES
    For i = 0 To 1
      If i = 0 Then
        'Left currency
        leftRightIdentifier = "quote"
        Curr = myws.Cells(4 + row, 5).Value
      Else
        'Right currency
        leftRightIdentifier = "base"
        Curr = myws.Cells(4 + row, 7).Value
      End If

      'Get the needed dropdown
      Set nodeCurrencyDropdown = doc.getElementById(leftRightIdentifier & "_currency_list_container")

      'Generate node collection of all currencies in dropdown
      Set nodeAllCurrencies = nodeCurrencyDropdown.getElementsByClassName("ltr_list_item")

      'Search the wanted currency in the single nodes
      For Each nodeOneCurrency In nodeAllCurrencies
        If InStr(1, nodeOneCurrency.innerText, Curr) > 0 Then
          Call TriggerEvent(doc, nodeOneCurrency, "mouseover")
          nodeOneCurrency.Click
          Exit For
        End If
      Next nodeOneCurrency
    Next i

    'Give a little time to calculate and get the result
    Application.Wait (Now + TimeValue("0:00:02"))
    myws.Cells(4 + row, 9).Value = doc.getElementById("base_amount_input").Value * 1

    'Next row
    row = row + 1
  Loop

  'Clean up
  ie.Quit
  Set ie = Nothing
  Set doc = Nothing
  Set nodeCurrencyDropdown = Nothing
  Set nodeAllCurrencies = Nothing
  Set nodeOneCurrency = Nothing

  'Show needed time
  MsgBox "Currencies have been converted" & vbNewLine & "Time Taken - " & Format((Timer - starttime) / 86400, "hh:mm:ss")
End Sub

這個 Sub() 觸發 HTML 事件:

Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)

  Dim theEvent As Object

  htmlElementWithEvent.Focus
  Set theEvent = htmlDocument.createEvent("HTMLEvents")
  theEvent.initEvent eventType, True, False
  htmlElementWithEvent.dispatchEvent theEvent
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM