how do i click on this button (with no id?)

chuchu

New Member
Joined
Jan 1, 2018
Messages
10
hi team

<button type="button" class="chakra-button css-imdvsq">Add Corporates</button>

i have this button here which i would like to click on to access another screen in VBA. I have no idea how to reference it, would greatly appreciate anybodies help and give extra smile :) faces!

cheers
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
That depends on if you're using IE(Internet Explorer) to achieve this..

If you're using IE

VBA Code:
Sub chuchu_webScrapper()
    On Error GoTo ErrHandle
   
    Dim URL As String
   
    Set ie = CreateObject("InternetExplorer.Application")

    URL = ""  '' insert URL here  ************************************************  <-------  <-------  <-------
   
       
    ie.Navigate URL
    ie.Visible = True
    While ie.readyState <> 4 Or ie.Busy: DoEvents: Wend

    Set ButtonsElement = ie.document.GetElementsByTagName("button")
    For i = 1 To ButtonsElement.Length - 1  ' loop through all buttons 
        If ButtonsElement.Item(i).innerHtml = "Add Corporates" Then  ''' Find Button by innerHTML
            ButtonsElement.Item(i).Click  '  <--- click button
        End If
    Next
   
    ie.Quit
    Set ie = Nothing

ErrHandle:
    ie.Quit
    Resume
End Sub


If you've already got some code down in the works repurpose what you see above to fit what you need :)
 
Upvote 0
That depends on if you're using IE(Internet Explorer) to achieve this..

If you're using IE

VBA Code:
Sub chuchu_webScrapper()
    On Error GoTo ErrHandle
  
    Dim URL As String
  
    Set ie = CreateObject("InternetExplorer.Application")

    URL = ""  '' insert URL here  ************************************************  <-------  <-------  <-------
  
      
    ie.Navigate URL
    ie.Visible = True
    While ie.readyState <> 4 Or ie.Busy: DoEvents: Wend

    Set ButtonsElement = ie.document.GetElementsByTagName("button")
    For i = 1 To ButtonsElement.Length - 1  ' loop through all buttons
        If ButtonsElement.Item(i).innerHtml = "Add Corporates" Then  ''' Find Button by innerHTML
            ButtonsElement.Item(i).Click  '  <--- click button
        End If
    Next
  
    ie.Quit
    Set ie = Nothing

ErrHandle:
    ie.Quit
    Resume
End Sub


If you've already got some code down in the works repurpose what you see above to fit what you need :)
Naive question

If I am using edge how would I change the formula above
 
Upvote 0
are you using selenium?

If so... something like this will work

VBA Code:
bot.FindElementByXPath("//button[text()='Add Corporates']", timeout:=-1, Raise:=False).Click
 
Upvote 0
are you using selenium?

If so... something like this will work

VBA Code:
bot.FindElementByXPath("//button[text()='Add Corporates']", timeout:=-1, Raise:=False).Click
obviously change "bot" to whatever your selenium driver is declared as
 
Upvote 0
Oh man

Might a bit above me :p I have to declare the selenium driver first?
Didn't you say you were using Edge? I assumed you were already using Edge... as far as I know there are two ways to automate Edge using VBA

the easiest way being Selenium

If you can show me what you already have (remove confidential information obviously) I could help further
 
Upvote 0
Public Sub Selenium()

Dim ie As Object
Set ie = CreateObject("Shell.Application")

ie.ShellExecute "https://stagging.knectx.com"



Set ButtonsElement = ie.document.GetElementsByTagName("button")
For i = 1 To ButtonsElement.Length - 1 ' loop through all buttons
If ButtonsElement.Item(i).innerHtml = "Add Corporates" Then ''' Find Button by innerHTML
ButtonsElement.Item(i).Click ' <--- click button
End If
Next



End Sub



__________

sadly this is as far as i got, i can open the website but nothing else works after this :(

i have selenium installed in tools reference
 
Upvote 0
i tried this as well

Dim ele As Object
For Each ele In HTMLDoc.getElementsByClassName("chakra-button css-imdvsq")
If ele.innerText = "Add Corporates" Then
ele.Click
Exit For
End If
Next ele

but says an object is required
 
Upvote 0
at this location
"Add Corporates"
does not exsist...
I believe you are trying to automate the process of adding a client to your account. To do that first you'll need to ensure you login to your account, add the customer/company information in the form(fairly easy process) and then click on the add corporates button... go ahead and use this to get there

VBA Code:
Private Sub chuchu_webScrapper_V2()
    On Error GoTo Errhandle
    Dim bot As New Selenium.WebDriver
    Dim emailID As String, passWord As String
 
 
 
    emailID = "[EMAIL]something@gmail.com[/EMAIL]" ''' set your login email here
    passWord = "123" ''' set your password here
 
    'Since you are adding sensitive information here I highly advice that you take steps to secure your code... including but not exclusive to protecting(locking) your VBA project
 
 
    With bot
        .Start "chrome", "[URL]https://stagging.knectx.com[/URL]"
        .Window.Maximize
        .Get "/"
        .Timeouts.Server = -1                    ' 10 mins
        .Timeouts.PageLoad = -1                  ' 10 mins
        .Timeouts.ImplicitWait = -1              ' 10 mins
        .Timeouts.Script = -1

    End With
 
    bot.FindElementByXPath("//a[text()='Log in']", timeout:=-1, Raise:=False).Click
    bot.FindElementById("field-2").SendKeys (emailID)
    bot.FindElementById("field-3").SendKeys (passWord)
    bot.FindElementByXPath("//button[text()='Sign In']", timeout:=-1, Raise:=False).Click
 
Errhandle:
    Resume '' change this to suit your needs I set it to resume so it doesn't bother you even if you don't change it .. but I do think you should have some form of error handling for this
End Sub

I can take you this far with what I know/assume.. but I think this is a good space to be in.. follow the coding patterns and you will be able to get to your goal before long :)
 
Upvote 0

Forum statistics

Threads
1,215,165
Messages
6,123,391
Members
449,098
Latest member
ArturS75

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top