刚开始使用 html,在 vba 中相当有能力,但在连接两者时遇到了一些问题。
我已经通过一个网站注册并试图获得结果。到目前为止使用的代码
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub GetVehicleDetails()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
Dim x As Integer
On Error GoTo Err_Clear
MyURL = "http://www.1stchoice.co.uk/find-a-part"
x = 0
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
MyBrowser.navigate MyURL
MyBrowser.Visible = True
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
HTMLDoc.all.license_plate.Value = "LV11VYT"
For Each MyHTML_Element In HTMLDoc.getElementsByTagName("button") '("input")
'Get 2nd button
If MyHTML_Element.Title = "Continue" Then 'MyHTML_Element.Click: Exit For
x = x + 1
If x = 2 Then
MyHTML_Element.Click
End If
End If
Next
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
现在我需要等到页面刷新然后得到结果,但我不确定如何把结果拉出来
源代码是
<div id="block_subheader" class="block_editable block_wysiwyg">
<p>Almost there! <strong>TELL US</strong> which parts you need - <strong>ADD </strong>your contact details & receive <strong>No Obligation Quotes</strong><span style="font-weight: normal;"> to compare & </span><span style="font-weight: normal;"><strong>Save ££'s!</strong></span></p>
</div>
<div class="clear"></div>
<form id="step3" action="/find-a-part/step-3" method="post" enctype="multipart/form-data">
<div class="clearfix">
<h2>RENAULT MEGANE (X95) DYNAMIQUE TOMTOM DCI ECO 3 DOOR COUPE 1461cc (2011) DIESEL</h2>
<p><a href="/find-a-part/step-2">Not quite the vehicle you're searching for? Click here to specify the vehicle exactly</a></p>
</div>
试图获得雷诺梅甘娜的详细信息
有人可以帮忙吗?
好的,我已经通过了这部分,但遇到了另一个问题,当单击按钮后页面发生更改时,我需要将 html.document 更新到新页面,因为当我在代码中使用它时,它会提取旧的源代码.
我可以让它工作,但它只适用于激活一个消息框来说明浏览器名称是什么。
有什么建议么?
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub GetVehicleDetails2()
Dim MyHTML_Element As IHTMLElement
Dim HTMLDoc As HTMLDocument, Doc As HTMLDocument
Dim MyURL As String, Vehicle As String
Dim x As Integer, y As Integer
On Error GoTo Err_Clear
MyURL = "http://www.1stchoice.co.uk/find-a-part"
x = 0
'open new explorer
Set MyBrowser = New InternetExplorer
MyBrowser.Silent = True
'navigate to page
MyBrowser.navigate MyURL
MyBrowser.Visible = True
'wait until ready
Do While MyBrowser.Busy Or _
MyBrowser.readyState <> 4
DoEvents
Loop
Do
Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
Set HTMLDoc = MyBrowser.document
'enter registration in text box
HTMLDoc.all.license_plate.Value = "LV11VYT"
'click continue button
Set MyHTML_Element = HTMLDoc.getElementsByTagName("button")(1)
MyHTML_Element.Click
Set HTMLDoc = Nothing
'wait until page updated
Set Doc = MyBrowser.document
'Application.Wait (Now() + "00:00:05")
'does not work if you take this out
MsgBox MyBrowser.FullName
'find text returned with vehicle details
For Each MyHTML_Element In Doc.getElementsByTagName("form")
If MyHTML_Element.ID = "step3" Then
Vehicle = MyHTML_Element.innerText
MsgBox Vehicle
End If
Next
'close browser down
'MyBrowser.Quit
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
使用 2003 或 2007,尝试网络查询,无法传递值并使用继续按钮。