简体   繁体   English

填写 Web 表格 - Excel VBA - Z9E13B69D1D2DA927102ACAAAF7154A3

[英]Fill out Web form - Excel VBA - Javascript

I am trying to fill out a web form where I want to select the following options from different lists:我正在尝试填写 web 表格,我想在其中 select 来自不同列表的以下选项:

  • the base currency to US Dollars,以美元为基础货币,
  • the target currencies to European Euros and British Pounds目标货币为欧洲欧元和英镑
  • Time horizon to Exact Time Period时间范围到确切时间段
  • Start date and End date to 5 May 2020开始日期和结束日期至 2020 年 5 月 5 日
  • Table Style to Microsoft Excel Microsoft Excel 的表格样式

After that, I need to click on the 'Retrieve data' button.之后,我需要单击“检索数据”按钮。

I can navigate to the website and click on the Retrieve Data button, but I can't select the values from the lists.我可以导航到该网站并单击“检索数据”按钮,但我不能 select 列表中的值。 Any help would be greatly appreciated!任何帮助将不胜感激!

Excel VBA code: Excel VBA 代码:

Sub FilloutWebForm()

    Dim ie As Object

    Set ie = CreateObject("internetexplorer.application")

    With ie
        .Visible = True
        .navigate "https://fx.sauder.ubc.ca/data.html"

        Do While .Busy
            DoEvents
        Loop

        Do While .readyState <> 4
            DoEvents
        Loop
    End With

'Fill out form

ie.document.querySelector("[href*='base']").Value = "USD"
ie.document.querySelector("[href*='targets']").Value = "EUR"
ie.document.querySelector("[href*='horizon']").Value = ""
ie.document.querySelector("[href*='time']").Value = "??"
ie.document.querySelector("[href*='tables']").Value = "Excel"

'Click Retrieve Data Button

Set Input_Elements = ie.document.getElementsByTagName("input")
    For Each Input_Element In Input_Elements
        If Input_Element.getAttribute("value") = "Retrieve Data" Then
            Input_Element.Click
            Exit For
        End If
    Next Input_Element

End Sub

Relevant web code相关web代码

 <a href="javascript:helpme('base');">Base Currency</a><br>
   <font size="-1">(choose one; most popular<br>choices appear at the top)</font>
   <select name="b">
      <option value="USD">U.S. Dollars
   <option value="CAD">Canadian Dollars
   <option value="EUR">European Euros
   <option value="GBP">British Pounds
   <option value="JPY">Japanese Yen

<a href="javascript:helpme('targets');">Target Currencies</a><br>
   <font size="-1">(choose one or more)</font>
   <br>
   <select name="c" multiple size=10>
      <option value="USD">U.S. Dollars
   <option value="CAD">Canadian Dollars
   <option value="EUR">European Euros
   <option value="GBP">British Pounds

 <a href="javascript:helpme('horizon');">Choose Time Horizon</a>
   <br>
   <select name="rd">
   <option value="" selected>Exact Time Period
   <option value="1">Last Trading Day
   <option value="7">Last 7 Days
   <option value="28">Last 28 Days

  <a href="javascript:helpme('horizon');">Choose Time Horizon</a>
   <br>
   <select name="rd">
   <option value="" selected>Exact Time Period
   <option value="1">Last Trading Day
   <option value="7">Last 7 Days
   <option value="28">Last 28 Days
   </select><br>&nbsp;<br>
   To use start and end date,<br>
   select "Exact Time Period" in the<br>
   "Choose Time Horizon" menu.<br>
   <a href="javascript:helpme('time');">Start Date</a><br>
   <input type=text size=2 name="fd" value="1" maxlength=2>
   <select name="fm">
   <option selected value="1">Jan
   <option value="2">Feb
   <option value="3">Mar
   <option value="4">Apr
   <option value="5">May
   <option value="6">Jun
   <option value="7">Jul
   <option value="8">Aug
   <option value="9">Sep
   <option value="10">Oct
   <option value="11">Nov
   <option value="12">Dec
   </select>
   <input type=text size=4 name="fy" value="2019" maxlength=4>
   <br>&nbsp;<br>
   <a href="javascript:helpme('time');">End Date</a><br>
   <input type=text size=2 name="ld" value="31" maxlength=2>
   <select name="lm">
   <option value="1">Jan
   <option value="2">Feb
   <option value="3">Mar
   <option value="4">Apr
   <option value="5">May
   <option value="6">Jun
   <option value="7">Jul
   <option value="8">Aug
   <option value="9">Sep
   <option value="10">Oct
   <option value="11">Nov
   <option selected value="12">Dec
   </select>

 <a href="javascript:helpme('tables');">Table Style</a>
   <br>
   <select name="f">
   <option value="HTML">HTML
   <option value="HTML2" selected>HTML+CSS
   <option value="plain">plain text
   <option value="Excel">Microsoft Excel
   <option value="LaTeX">LaTeX table
   <option value="csv">CSV spreadsheet
   <option value="tab">Tab spreadsheet
   </select>

There is nothing to do with JavaScript.与 JavaScript 无关。 The links you try to click open the help texts.您尝试单击的链接会打开帮助文本。 You must fill the input tags directly.您必须直接填写输入标签。 It's no good idea to ask for an Excel File, because you can't handle the following download dialog.请求 Excel 文件不是一个好主意,因为您无法处理以下下载对话框。 It's better to ask for a html table.最好要一张html表。 You can read out the data from it.您可以从中读出数据。

Sub FilloutWebForm()

  Dim ie As Object
  Dim nodeAllInput As Object
  Dim nodeOneInput As Object

  Set ie = CreateObject("internetexplorer.application")
  With ie
    .Visible = True
    .navigate "https://fx.sauder.ubc.ca/data.html"
    Do While .readyState <> 4: DoEvents: Loop
  End With

  'Fill out form
  'Base currency to US Dollars
  ie.document.getElementsByName("b")(0).Value = "USD"

  'Target currencies European Euros and British Pounds
  With ie.document.getElementsByName("c")(0)
    .Children(2).Selected = True
    .Children(3).Selected = True
  End With

  'Time horizon to Exact Time Period
  ie.document.getElementsByName("rd")(0).Value = ""

  'Start date
  ie.document.getElementsByName("fd")(0).Value = "5"
  ie.document.getElementsByName("fm")(0).Value = "5"
  ie.document.getElementsByName("fy")(0).Value = "2020"

  'End date
  ie.document.getElementsByName("ld")(0).Value = "5"
  ie.document.getElementsByName("lm")(0).Value = "5"
  ie.document.getElementsByName("ly")(0).Value = "2020"

  'Table Style to HTML
  ie.document.getElementsByName("f")(0).Value = "HTML"

  'Click Retrieve Data Button
  Set nodeAllInput = ie.document.getElementsByTagName("input")
  For Each nodeOneInput In nodeAllInput
    If nodeOneInput.getAttribute("value") = "Retrieve Data" Then
      nodeOneInput.Click
      Exit For
    End If
  Next nodeOneInput
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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