【发布时间】:2019-08-08 16:16:42
【问题描述】:
我正在使用 vba 并尝试在此网站中填写表格并获取输出 Link Here
当我尝试填写从/到机场的输入框时出现问题。这是我尝试过的:正在调用此函数来填写往返机场字段
Function enter_get_name(ByVal iedoc As HTMLDocument, _
ByVal input_box As String, ByVal iata As String, _
ByVal id As String, ByRef str As Variant) As Boolean
Dim noopt As Integer ' length of string that appear on drop down menu if no option available
noopt = Len("If your destination does not appear among the cities listed in the destination box")
iedoc.getElementsByName(input_box)(0).innerText = iata ' enter string
Set drop_down = iedoc.getElementById(id).getElementsByTagName("li")
Do While drop_down.Length = 0: DoEvents: Loop ' wait for the drop down menu to come up
If Len(drop_down(0).innerText) = noopt Then ' if option do not exist
enter_get_name = False ' return value
Exit Function ' exit
Else
For Each Name In drop_down ' loop all options of drop down menu
' if found a exact same IATA code, click that html element
str = Mid(Name.innerText, Len(Name.innerText) - 4, 3)
If StrComp(iata, str, 1) = 0 Then
Name.Click
Exit For
End If
Next
enter_get_name = True
End If
End Function
所以我尝试循环下拉列表中的所有可用选项,找到该元素,然后单击它。该代码可以成功找到该元素,但是当我尝试单击该元素时,它有时不起作用。例如,我有一个从 HKG 到 SIN 的航班作为输入。
到达(TO)机场有2个选项:HEL和SIN,它以某种方式点击了HEL。但是,如果我反过来做,即:从 SIN 到 HKG,选择具有 10 多个选项的 SIN 没有问题。我该如何解决这个问题?任何帮助将不胜感激。
【问题讨论】:
-
它不起作用。两个答案都是选择下拉菜单上的第一个选项。
标签: excel vba dom web-scraping html-table