如何使用 VBA 在 Internet Explorer 页面上选择单选按钮?



我正在尝试打开一个网站并使用VBA选择一个单选按钮。 我的错误消息显示"对象'IWebBrowser2'的方法'文档'失败"。

我尝试单击的按钮的 ID 是 : id=ctl00_ContentPlaceHolder1_rbtnRequestForSomeoneElse

这是我的 VBA:

Sub Upload()
Dim url As String
url = "http://radiobuttonlocation.com"
Set IE = CreateObject("InternetExplorer.application")
IE.Visible = True
IE.Navigate url
Do While IE.ReadyState = 4: DoEvents: Loop

IE.Document.getElementById("ctl00_ContentPlaceHolder1_rbtnRequestForSomeoneElse").Click

End Sub

所以IE在导航时往往会迷失自己,尝试再次获取网页。

*这需要参考 Microsoft Shell 控件和自动化,Microsoft互联网控件

Sub Upload()
Dim url As String
url = "http://radiobuttonlocation.com"
Set IE = CreateObject("InternetExplorer.application")
IE.Visible = True
IE.Navigate url
Do While IE.ReadyState = 4: DoEvents: Loop
'get the webpage again
set IE = GetWebPage(url)
IE.Document.getElementById("ctl00_ContentPlaceHolder1_rbtnRequestForSomeoneElse").Click

End Sub

Public Function GetWebPage(sUrl As String) As InternetExplorer
Dim winShell As Shell
Dim dt As Date
dt = DateAdd("s", 300, DateTime.Now)
Dim ie As InternetExplorer
Do While dt > DateTime.Now
Set winShell = New Shell
'loop through the windows and check the internet explorer windows
For Each ie In winShell.Windows
If ie.LocationURL = sUrl Then
ie.Visible = True
ie.Silent = True
Set GetWebPage = ie
Do While ie.Busy
DoEvents
Loop
Exit Do
Set winShell = Nothing
End If
Next ie
Set winShell = Nothing
DoEvents
Loop
End Function

最新更新