Clicking a button in a website

Nevermore

New Member
Joined
Sep 12, 2013
Messages
12
Hi,

I was accessing a webpage "Convert Lat Long to Address Show on Map" and wanted to to pull the information from this website.

After putting the Latitude and Longitude values in the respective places I am not able to initiate the click button "Convert to Address" through a VBA code.

How can I click the button using VBA Marco.

Thanks
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Can you post what code you have?

Code:
Sub BMTC()


Dim IE As New InternetExplorer
Dim LAT
Dim LON




IE.Visible = True
IE.navigate ("http://www.latlong.net/Show-Latitude-Longitude.html")


Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE


LAT = ThisWorkbook.Sheets(2).Range("B2").Value
LON = ThisWorkbook.Sheets(2).Range("C2").Value


Dim Srce As HTMLDocument
Set Srce = IE.document
Srce.getElementById("lat").Value = LAT
Srce.getElementById("lng").Value = LON


'''The below code where I am having the problem with'''
Srce.getElementById("BUTTON").Click


End Sub
 
Upvote 0
The button's ID isn't ' button'.

Try this.
Code:
Option Explicit

Sub BMTC()
Dim IE As New InternetExplorer
Dim Srce As HTMLDocument
Dim btn As Object
Dim LAT
Dim LON

    IE.Visible = True
    IE.navigate ("http://www.latlong.net/Show-Latitude-Longitude.html")

    Do
        DoEvents
    Loop Until IE.readyState = READYSTATE_COMPLETE

    LAT = ThisWorkbook.Sheets(2).Range("B2").Value
    LON = ThisWorkbook.Sheets(2).Range("C2").Value

  
    Set Srce = IE.document
    Srce.getElementById("lat").Value = LAT
    Srce.getElementById("lng").Value = LON

    Set btn = Srce.getElementsByTagName("Button")(0)

    btn.Click

End Sub
 
Upvote 0
Dear Norie,

First of all 'Thank you so much' for you help.

It is working but I wanted to know how did you figure it out. Some doubts that I have in my mind is like:
1. Why couldn't we just put the code as "Srce.getElementsByTabName("Button")(0).Click", why did you assign it to an object 'btn'?
2. What does the (0) signify in the end of the code?

Thanking you for the help.
 
Upvote 0
1 By using an object I could actually check if the code was working.

For example, if getElementsByTagName("button") hadn't worked btn would be Nothing.

I could check for that in the Locals Window as I stepped through the code.

2 getElementsByTagName returns a collection of elements.

The collection's index is zero-based, so the index of the first item in it is 0.

Items in the collection can be accessed using Item(index), or you can use a shortcut (index[/]) as I did.

The button you want to click is the first (and only actually) item in the collection so we can create a reference to it with this.
Code:
    Set btn = Srce.getElementsByTagName("Button")(0)

We can then click the button.:)
 
Upvote 0
Dear Norie,

Thank you so much for all your time and the description that you have provided.

I had one last question regarding the code.

Is there a way to know whether the item is a collection or not so that I can know when to use the reference (i.e. Index) for the item?
 
Upvote 0
getElementsByTagName will always return a collection if it finds elements with the specified tag.

If doesn't find anything it will be Nothing.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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