Home » excel » excel – How do i grab a specific element from html with vba using a selenium wrapper

excel – How do i grab a specific element from html with vba using a selenium wrapper

Posted by: admin May 14, 2020 Leave a comment

Questions:

How can i call just the 8th cell of a table from html using vba with a selenium wrapper? I am trying to print the date of these impounds into cells in excel. Here is an example where the search has returned the vehicle being impounded 3 times with 3 different dates.

view-source: https://www.autoreturn.com/indianapolis-in/find-vehicle/results

I have tried a couple different variatons of .Findelement such as

Sheets("VinCheck").Cells(i, "D").Value = chromeDriver.FindElementByClass("input-item").Text

but nothing seems to be working. This just returns a seemingly blank value.

How to&Answers:

You can use pseudo class selector of :nth-of-type to specify the column number (nth td cell within a row)

Option Explicit
Public Sub SearchVin()
    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/"

    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
        Dim towDates As Object, towDate As Object
        If Not hTable Is Nothing Then
            Set towDates = .FindElementsByCss("table tr.results-row td:nth-of-type(9)")
            For Each towDate In towDates
                Debug.Print towDate.Text
            Next
        End If
        .Quit
    End With
End Sub

Results look like:


Restricting row

You can of course add another nth-of-type selector to restrict the row retrieved as well. Say you wanted the tow datetime for row 2:

table tr:nth-of-type(2).results-row td:nth-of-type(9)

Note that as I am using a class "." selector as well then I am limiting returned rows in the above selectors to exclude the header row.


Using last-child:

As the desired column is the last td cell in a row (last column), you can use a :last-child pseudo class selector instead of :nth-of-type e.g.

Set towDates = .FindElementsByCss("table tr.results-row td:last-child")

and

table tr:nth-of-type(2).results-row td:last-child

Single return value versus list:

If you are expecting a single value then you apply the css selector with

.FindElementByCss

e.g.

.FindElementsByCss("table tr:nth-of-type(2).results-row td:last-child")

If you are expecting a list of values then you apply the css selector with

.FindElementsByCss

e.g.

.FindElementsByCss("table tr.results-row td:last-child")

first-child selector:

Also worth noting you have access to :first-child selector, for example to grab first result row.


Writing out entire table:

If you want to write out the entire table I refer you to my answer here.