如何在VBA Access的网页中选择组合框?

时间:2018-07-13 21:18:18

标签: vba web-scraping access-vba

我一直在尝试许多方法来尝试在网页的组合框中选择一个选项。我真的很困

Set AllHyperLinks = ie.Document.getElementsByTagName("A")
For Each hyper_link In AllHyperLinks

If hyper_link.innerText = "UMSCP Phoenix 928" Then
hyper_link.Click
Exit For
End If
Next

Do
DoEvents
Loop Until ie.ReadyState = 3

Do
DoEvents
Loop Until ie.ReadyState = 4
' TROUBLE START HERE

ie.Document.getElementById("ProgramID").Value = "1146|928 Phoenix TouchPoints"

Set objButton = ie.Document.getElementById("ProgramID")
    objButton.focus
    objButton.Click

这是网页源代码

<a href="#" id="changeimage" style="color: rgb(91, 108, 77); font-size: 13px; display: none;" onclick="ShowProgramChange(895,1)">928 Phoenix Legacy  </a>
<div style="display: inline;" id="programlistbox"><divabc><select onclick="ProgramID_onchange();" language="javascript" style="" name="ProgramID" id="ProgramID">
<option value="895|928 Phoenix Legacy" selected="">928 Phoenix Legacy</option>
<option value="1146|928 Phoenix TouchPoints">928 Phoenix TouchPoints</option>
</select>
</divabc></div>

1 个答案:

答案 0 :(得分:0)

请尝试以下操作以更改选项。似乎HTML有一条导致IE8仿真的语句,它不支持querySelector(我认为)。有很多ASP方面的工作正在进行,所以这可能很棘手。

还要确保<!doctype html>位于文档顶部。

Dim html As HTMLDocument
Set html = ie.document

Dim findString As String, replaceString As String
findString = "IE=EmulateIE8"
replaceString = "IE=edge"

ie.document.body.innerHTML = Replace(ie.document.body.innerHTML, findString, replaceString)
ie.document.querySelector("option[value='1146|928 Phoenix TouchPoints']").Selected = True
'    ie.document.getElementById("ProgramID").getElementsByTagName("option")(1).Selected = True '< Or this

并触发事件:

ie.document.getElementById("ProgramID").Click
ie.document.getElementById("ProgramID").FireEvent "onclick"

您也可以尝试:

IE.Document.parentWindow.execScript "ShowProgramChange(1146,1); "

并使用selenium basic(安装并添加对硒类型库的引用)

Public Sub test()
 Dim d As New IEDriver
    With d
        .Start
        .get "youURL"
        'Other code
        .FindElementByXPath("//table/tbody//option").AsSelect.SelectByValue ("1146|928 Phoenix TouchPoints")
        'Other code
        .Quit
    End With
End Sub