我会给硒基本包装 https://github.com/florentbr/SeleniumBasic/releases/tag/v2.0.9.0如果允许安装,请尝试使用 vba。安装后,您可以通过 vbe > 工具 > 引用添加对 selenium 类型库的引用。您需要安装最新的 chrome 和 chromedriver,并且 chromedriver.exe 应与 selenium 可执行文件位于同一文件夹中。
那么你的任务的语法就很好并且具有描述性。我没有在 vins 上添加循环,但显示了搜索的基本元素。我提供了一个子程序来将结果写到工作表中。
我希望能够删除 SendKeys 之后的显式等待,但似乎没有任何页面事件/更改我可以监视以确定何时单击按钮并包含发送的 vin。 1 秒的等待似乎始终足够。您可以根据您执行的搜索次数探索减少此次数。
Option Explicit
Public Sub SearchVins()
Dim d As WebDriver, hTable As Object, ws As Worksheet, t As Date
Dim headers(), vin As String
Const MAX_WAIT_SEC As Long = 10
Set d = New ChromeDriver
Set ws = ThisWorkbook.Worksheets("Sheet1")
Const URL = "https://www.autoreturn.com/indianapolis-in/find-vehicle/"
headers = Array("License", "State", "Make", "Model", "Color", "Vin", "Status", "Tow date and Time")
vin = "1G4HD57287U218052"
With d
.Start "Chrome"
.get URL
.FindElementById("vin").SendKeys vin '<== vin
Application.Wait Now + TimeSerial(0, 0, 1)
.FindElementByCss("[onclick='submitVin()']").Click
t = Timer
Do
DoEvents
On Error Resume Next
Set hTable = .FindElementByCss("table") 'use tag name of results table to target table
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While hTable Is Nothing
'do something with results
If Not hTable Is Nothing Then
WriteTable hTable, LastRow(ws) + 2, ws
Set hTable = Nothing
End If
.FindElementByCss("a.more-link").Click '<== search again button
'Another search for example.....
Stop '<==Delete me later
ws.Cells(2, 2).Resize(1, UBound(headers) + 1) = headers '<== Finally add headers
.Quit
End With
End Sub
Public Sub WriteTable(ByVal hTable As Object, ByVal startRow As Long, ByVal ws As Worksheet)
Dim tr As Object, td As Object, r As Long, c As Long
r = startRow
For Each tr In hTable.FindElementsByTag("tr")
c = 1
For Each td In tr.FindElementsByTag("td")
ws.Cells(r, c) = td.Text
c = c + 1
Next
r = r + 1
Next
End Sub
Public Function LastRow(ByVal sh As Worksheet) As Long
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function