【问题标题】:Why is SendKey Enter not working with Chrome browser为什么 SendKey Enter 不能与 Chrome 浏览器一起使用
【发布时间】:2019-01-15 19:08:47
【问题描述】:

我正在尝试检查在 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

【问题讨论】:

  • 你考虑过为 vba 使用 selenium basic wrapper 吗?
  • 试试不带括号的Application.SendKeys "~", True
  • @JoshEller - 或者,{ENTER}
  • 我已经尝试使用 {ENTER},但它似乎也没有注册。
  • 我刚刚尝试了不带括号的~,它成功了,非常感谢!

标签: excel vba web-scraping


【解决方案1】:

如果您被允许安装,我会尝试selenium basic wrapper for vba。安装后,通过 vbe > tool > references 添加对 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

【讨论】:

  • 非常感谢您的回复。这是 1G4HD57287U218052 的 vin 示例。
  • 我添加了一个提交和检索值的示例。
猜你喜欢
  • 2016-07-08
  • 2019-12-13
  • 2015-04-14
  • 2021-04-03
  • 2013-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-01
相关资源
最近更新 更多