There is nothing to do with 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. It’s better to ask for a html table. 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
I am trying to fill out a web form where I want to select the following options from different lists:
- 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
- Table Style to 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.
Any help would be greatly appreciated!
Excel VBA code:
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
<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> <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> <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>
Do you wanna parse the data or download the file? If you wish to scrape the data, you can simply send a post http requests with appropriate parameters and get the data with the blink of an eye.