2

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.

2
  • Sounds like you might need to use Selenium.actions to string a couple actions together which is needed often when dropdowns are involved. I'm not so familiar with VBA, but maybe that bit of direction helps. Commented Feb 13, 2019 at 22:42
  • I take back my previous comment, I think Kajal's selector adjustments are probably all you need. Update us if not :) Commented Feb 13, 2019 at 22:51

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

The first one results in a Run-time Error '32': Invalid Selector Error. The other results on Run-time Error '438': Object doesn't support this property or method.
I have modified my answer please try now.
Thanks a lot for the help so far. The first one seems to be working to select and click the button but my spreadsheet data is not updating correctly. For example before changing it to today's date it defaults to show the last 30 days and that is the data that it is pulling currently (with your answer), not the new updated information that should auto-fill once 'Today' is selected and the button "Apply' is clicked. What would be the best way to delay grabbing the data until it auto-fills in 'Today' information. For further details here is the other relevant HTML: pastebin.com/X1N2SYLz
Ok.Let me check.Could you please accept my answer by clicking accept button.
You need to wait for the element to be present in page or you can put some implicit delay. I don't know how to make it selenium VBA.I am good at selenium Java & Python.I have to check for the syntax then only able to say something.As per you said unable to click the button I had tried to help to you.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.