Click button on Webpage without ID

Moeey

New Member
Joined
Sep 13, 2014
Messages
34
Hello,
I am new to this site but I have used it extensively over the last year or so to teach myself how to use VBA in the Excel environment.
It has been great fun learning, and this site has helped me a lot.
It has been relatively easy to find solutions to my problems up till now, because I havn't had to integrate with other environments.
But my latest project is to try and automate login to a website and extract reports to excel spreadsheets.
I apologise if the problem seems stupid and easy to some people, but I'm no expert. I have tried to find the solution by browsing through forums but I can't seem to find anything specific to this. And anything I do find which resembles it, I cannot fully understand.
Ok, so here's the problem, I have a webpage which has a button that does not have an ID, so I don't know how to click the button.
This webpage is in IE8.
This is the HTML code behind the button.

<a class="linkbutton_list" href="javascript:__doPostBack('ctl00$ContentBody$rptSites$ctl00$ctl00','')">

Please let me know if you need more information regarding the problem, as I wasn't sure how much info to provide.
Any solutions which are not too compliacted would be greatly appreciated.
Thanks
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hello,
Apologies(I'm new to this) but here's the missing HTML code behind the button

Code:
a class= "linkbutton_list" href= "javascript:__doPostBack('ctl00$ContentBody$rptSites$ctl00$ctl00','')"
 
Upvote 0
Is that the whole A tag? You could try something like this, but it assumes the link you want contains the first occurrence of the ct100 string.
Code:
Public Sub Test()
    
    Dim IE As InternetExplorer
    Dim HTMLdoc As HTMLDocument
    Dim button As HTMLInputButtonElement
    Dim i As Integer
    
    Set IE = New InternetExplorer
    With IE
        .Visible = True
        .Navigate "http://www.bbc.co.uk"
        While .busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
        Set HTMLdoc = .Document
    End With
    
    '< a class= "linkbutton_list" href= "javascript:__doPostBack('ctl00$ContentBody$rptSites$ctl00$ctl00','')"
    
    Set button = Nothing
    i = 0
    While i < HTMLdoc.Links.Length And button Is Nothing
        If InStr(HTMLdoc.Links(i).href, "ctl00$ContentBody$rptSites$ctl00$ctl00") > 0 Then Set button = HTMLdoc.Links(i)
        i = i + 1
    Wend
    
    If Not button Is Nothing Then
        button.Click
    Else
        MsgBox "Button not found"
    End If
    
End Sub
In Tools - References, select MS Internet Controls and MS HTML Object Library.
 
Upvote 0
Thank you John_w, I will try this when I get a chance and get back with the results.
I'm a bit snowed under at work at the mo.
:)
 
Upvote 0
Thanks John your code worked a treat, but I had to add an extra line.
I ran the code initially and it just came up with the msgbox "Button not found". But when I put the code in debug it worked ok.
I did a search through the forum on the following "HTMLdoc.Links.Length" and found the following thread http://www.mrexcel.com/forum/excel-...plications-click-javascript-based-anchor.html which you(John) were involved in too.
I think the problem may have been to do with loading the pages.
The button in question is on a page after the login page.

So I added the following
Code:
Application.Wait Now + TimeValue("00:00:01")
and now it works ok.

So here's the complete code
Code:
Sub Login()
    
    Dim IE As InternetExplorer
    Dim HTMLdoc As HTMLDocument
    Dim button As HTMLInputButtonElement
    Dim i As Integer
    
    Set IE = New InternetExplorer
    With IE
        .Visible = True
        .navigate "[URL="http://esssmartweb01v/CCMWebLive/CCMLogin.aspx?ReturnUrl=%2fCCMWEBLIVE"]X[/URL]"
        While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
        Set HTMLdoc = .document
    End With
    
    Set NameEditB = HTMLdoc.getElementByID("ctl00$ContentBody$LoginControl1$txtUserName")
    Set PWDEditB = HTMLdoc.getElementByID("ctl00_ContentBody_LoginControl1_txtPassword")
    NameEditB.Value = "X"
    PWDEditB.Value = "X"
    Set SignIn = HTMLdoc.getElementByID("ctl00_ContentBody_LoginControl1_butLogon")
    SignIn.Click
    
    ' Delays the code execution to allow IE to catch up.
    Application.Wait Now + TimeValue("00:00:01")
    
    Set button = Nothing
    i = 0
    While i < HTMLdoc.Links.Length And button Is Nothing
        If InStr(HTMLdoc.Links(i).href, "ctl00$ContentBody$rptSites$ctl00$ctl00") > 0 Then Set button = HTMLdoc.Links(i)
        i = i + 1
    Wend
    
    If Not button Is Nothing Then
        button.Click
    Else
        MsgBox "Button not found"
    End If
    
End Sub
Once again thanks John for all your help.
Much appreciated:)
 
Last edited:
Upvote 0
Rather than the 1 second artificial wait, you could have done the IE busy readyState wait loop, which is the normal way of waiting after clicking a button or submitting a form which loads a new page.
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,202
Members
448,554
Latest member
Gleisner2

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