[英]Fill out Web form - Excel VBA - Javascript
我正在嘗試填寫 web 表格,我想在其中 select 來自不同列表的以下選項:
之后,我需要單擊“檢索數據”按鈕。
我可以導航到該網站並單擊“檢索數據”按鈕,但我不能 select 列表中的值。 任何幫助將不勝感激!
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
相關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> <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>
與 JavaScript 無關。 您嘗試單擊的鏈接會打開幫助文本。 您必須直接填寫輸入標簽。 請求 Excel 文件不是一個好主意,因為您無法處理以下下載對話框。 最好要一張html表。 您可以從中讀出數據。
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.