我正在尝试编写一个特定网站上的宏,粘贴一些输入,单击搜索按钮,然后从搜索结果中复制数据。 不幸的是,据我所知,搜索结果是动态准备的(在JavaScript中?)如果我不知道id,我不知道如何使用任何方法,如getElementById。
以下是网站:https://www.tracktrace.dsv.com/newtracking/login.jsp
这是在左侧搜索栏内容中搜索到的:DSVAO72630
我试图捕捉结果中出现的任何内容,例如。货件ID(在搜索结果的最顶部有SHPTS ID:SMMA0019991
)。到目前为止,这是我的代码:
Option Explicit
Sub Crawler()
Dim IE As InternetExplorerMedium
Dim URL As String
Dim HTMLDoc As HTMLDocument
Dim searchButton As Object
Dim searchBar As Object
Application.ScreenUpdating = True
Set IE = CreateObject("InternetExplorer.Application")
URL = "https://www.tracktrace.dsv.com/newtracking/login.jsp"
With IE
.Navigate URL
.Visible = True
End With
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Set HTMLDoc = IE.document
Set searchBar = HTMLDoc.getElementById("sid")
searchBar.Value = "DSVAO72630"
Set searchButton = HTMLDoc.getElementsByName("SEARCH")
searchButton.Item(0).Click
End Sub
答案 0 :(得分:2)
这是一种方式......
Option Explicit
Sub Crawler()
Dim IE As Object, HTMLDoc As Object, searchButton As Object
Dim searchBar As Object, tbl As Object
Dim URL As String
Dim Ar As Variant
Dim i As Long
Set IE = CreateObject("InternetExplorer.Application")
URL = "https://www.tracktrace.dsv.com/newtracking/login.jsp"
With IE
.Navigate URL
.Visible = True
End With
Do: DoEvents: Loop Until IE.readyState = 4
Set HTMLDoc = IE.document
Set searchBar = HTMLDoc.getElementById("sid")
searchBar.Value = "DSVAO72630"
Set searchButton = HTMLDoc.getElementsByName("SEARCH")
searchButton.Item(0).Click
Wait 2
Set tbl = HTMLDoc.getElementsByTagName("TABLE")(2)
Ar = Split(tbl.Cells(1, 2).outertext, vbNewLine)
For i = LBound(Ar) To UBound(Ar)
If InStr(1, Ar(i), "SHPTS ID:") Then
Debug.Print Trim(Split(Ar(i), ":")(1))
Exit For
End If
Next i
IE.Quit
Set IE = Nothing
End Sub
Private Sub Wait(ByVal nSec As Long)
nSec = nSec + Timer
While nSec > Timer
DoEvents
Wend
End Sub
<强>截图强>