我正在尝试检查在chrome浏览器中输入到excel的几个vins,此代码将打开浏览器并输入它们,但不会按Enter键来单击按钮。不知道我在做什么错,但我尝试了几种变体,似乎无法解决任何问题。
抱歉,如果我的格式很糟糕,这是我第一次在这里发布。
chromePath = """C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"""
StartRow = 2
EndRow = InputBox("Please enter how many vins to check!")
RowIndex = 2
EndRow = 1 + EndRow
For i = StartRow To EndRow
Vin = Sheets("VinCheck").Cells(i, "A")
browser = Shell(chromePath & " -url https://www.autoreturn.com/indianapolis-in/find-vehicle/ ")
Application.Wait Now + 0.00003
Application.SendKeys "{Tab}", True
Application.SendKeys "{Tab}", True
Application.SendKeys "{Tab}", True
Application.SendKeys "{Tab}", True
Application.SendKeys Vin, True
Application.SendKeys "{~}", True
Application.SendKeys "{Tab}", True
Application.Wait Now + 0.00003
Msg = "Was Vehicle Found?" & vbCrLf & "Click Yes to move on to the next Vin"
MsgBox Msg, vbYesNo, "Advanced Excel Training"
If Response = vnYes Then
Sheets("VinCheck").Cells(i, "B").Value = "Found"
Else
Sheets("VinCheck").Cells(i, "B").Value = "Vehicle Not Found"
End If
Next i
End Sub
答案 0 :(得分:1)
如果允许您安装,我会为vba尝试selenium basic wrapper。安装后,您可以通过vbe>工具>引用添加对硒类型库的引用。您需要安装最新版的chrome和chromedriver,并且chromedriver.exe应该与selenium可执行文件位于同一文件夹中。
然后,您的任务的语法就很好并且具有描述性。我没有在vins上添加循环,但是显示了搜索的基本元素。我提供了一个子程序,将结果写到工作表中。
我希望能够删除SendKeys之后的明确等待,但是似乎没有任何页面事件/更改,我可以监视该事件/更改以确定何时单击按钮并包含发送的vin。一秒钟的等待时间似乎足够了。您可以根据执行的搜索次数来尝试减少这种情况。
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