Home » excel » excel – How to target CSS button elements properly with VBA Selenium when they don't have any IDs?

excel – How to target CSS button elements properly with VBA Selenium when they don't have any IDs?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to target and click a button on a site that requires me to open up a dropdown menu, select the option on the list, and click Apply. I have gotten it most of the way there but I can’t seem to get it to click the button. Here is a link to the relevant HTML that I am working with https://pastebin.com/n5hJY3ua. And the full Xpath of the button is: /html/body/div[5]/div[3]/div/button[1]

I have tried various ways to target the button such as:

.FindElementByXPath("//div[@button='Apply']").Click

.FindElementByClass("applyBtn btn btn-small btn-success").Click

.FindElementByXPath(".//div[@button, 'Apply']").Click

Option Explicit

Public Sub ClickDate()
    Dim t As Date
    Dim ele As Object
    Dim driver As New ChromeDriver
    Dim post As WebElement
    Dim i As Integer
    Dim mysheet As Worksheet        

    Const MAX_WAIT_SEC As Long = 10
    Const INURL = "https://ss3.shipstation.com/#/dashboard"
    Const URL = "https://ss3.shipstation.com/"

    Set mysheet = Sheets("Sheet1")

    With driver '<==log into shipstation
        .Start "Chrome"
        .get URL
        t = Timer
        Do
            On Error Resume Next
            Set ele = .FindElementById("username")
            On Error GoTo 0
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While ele Is Nothing

        If ele Is Nothing Then Exit Sub
        ele.SendKeys "Username"
        .FindElementById("password").SendKeys "Password"
        .FindElementById("btn-login").Click
    End With


    With driver '<==select todays date
        .get INURL  
        Dim drf As Object
        t = Timer

        Do
            Set drf = driver.FindElementsByCss(".col-sm-4 h2")
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While drf.Count = 0

        If drf.Count > 0 Then
            .FindElementByClass("display-date").Click

            With .FindElementByXPath("//html/body/div/*/ul/li[1]")
                .Click
            End With

            .FindElementByXPath("//div[contains(@button, 'applyBtn btn btn-small btn-success')]").Click
        End If

        i = 2
        Dim item As Object, nodeList As Object, r As Long
        t = Timer

        Do
            Set nodeList = driver.FindElementsByCss(".col-sm-4 h2")
            If Timer - t > MAX_WAIT_SEC Then Exit Do
        Loop While nodeList.Count = 0

        If nodeList.Count > 0 Then
            For Each item In nodeList
                r = r + 1
                ActiveSheet.Cells(2, r) = item.Text
            Next
        End If
    End With
End Sub

I need it to click Apply causing the site to search Today’s Date and then grab the data and display it on an excel sheet. Everything is working except being able to click the button. It is returning to me

Run-time error ‘7’: NoSuchElementError.

How to&Answers:

Please try either of this two methods.It should work.

findElementByXPath("//button[contains(text(), 'Apply')]")

Or

findElementByXPath("//button[text()[contains(.,'Apply')]]")

Or

driver.findElementByCssSelector("button.applyBtn")

Please let me know if this work.

Answer:

You can reduce the css selector further and use a single class selector. Css selector method will be faster than xpath:

driver.findElementByCss(".applyBtn").click

The correct syntax is findElementByCss for VBA selenium basic