Use Excel Vba to Click on a Button in Internet Explorer, When The Button Has No "Name" Associated

Use Excel VBA to click on a button in Internet Explorer, when the button has no name associated

With the kind help from Tim Williams, I finally figured out the last détails that were missing. Here's the final code below.

Private Sub Open_multiple_sub_pages_from_main_page()

Dim i As Long
Dim IE As Object
Dim Doc As Object
Dim objElement As Object
Dim objCollection As Object
Dim buttonCollection As Object
Dim valeur_heure As Object

' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
' You can uncoment Next line To see form results
IE.Visible = True

' Send the form data To URL As POST binary request
IE.navigate "http://webpage.com/"

' Wait while IE loading...
While IE.Busy
DoEvents
Wend

Set objCollection = IE.Document.getElementsByTagName("input")

i = 0
While i < objCollection.Length
If objCollection(i).Name = "txtUserName" Then
' Set text for search
objCollection(i).Value = "1234"
End If
If objCollection(i).Name = "txtPwd" Then
' Set text for search
objCollection(i).Value = "password"
End If

If objCollection(i).Type = "submit" And objCollection(i).Name = "btnSubmit" Then ' submit button if found and set
Set objElement = objCollection(i)
End If
i = i + 1
Wend
objElement.Click ' click button to load page

' Wait while IE re-loading...
While IE.Busy
DoEvents
Wend

' Show IE
IE.Visible = True
Set Doc = IE.Document

Dim links, link

Dim j As Integer 'variable to count items
j = 0
Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
n = links.Length
While j <= n 'loop to go thru all "a" item so it loads next page
links(j).Click
While IE.Busy
DoEvents
Wend
'-------------Do stuff here: copy field value and paste in excel sheet. Will post another question for this------------------------
IE.Document.getElementById("DetailToolbar1_lnkBtnSave").Click 'save
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now) 'wait
Loop
IE.Document.getElementById("DetailToolbar1_lnkBtnCancel").Click 'close
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now) 'wait
Loop
Set links = IE.Document.getElementById("dgTime").getElementsByTagName("a")
j = j + 2
Wend
End Sub

IE click on a button that has no link associated to it (using Excel VBA)

It is within an input not a tag element so gathering a tags will not capture what you want. Using the id, as suggested in comments is one way to go. If you get element not found then check whether your element is within a parent frame or iframe which needs to be negotiated. General syntax for that is

ie.document.getElementsByTagName("frame")(appropriateIndexHere).contentDocument.getElementById("B91938241817236808")
ie.document.getElementsByTagName("iframe")(appropriateIndexHere).contentDocument.getElementById("B91938241817236808")

If the id is dynamic you can use the value attribute

ie.document.querySelector("[value='Créer']")
ie.document.getElementsByTagName("frame")(appropriateIndexHere).contentDocument.querySelector("[value='Créer']") 'etc

As there is an event, you may need to fire it.

ie.document.querySelector("[value='Créer']").FireEvent "onclick"
ie.document.getElementsByTagName("frame")(appropriateIndexHere).contentDocument.querySelector("[value='Créer']").FireEvent "onclick" 'etc

And use a proper page load wait. So this,

Do While IE.Busy                                                     'loading page
Application.Wait DateAdd("s", 1, Now)
Loop

Should be

While ie.Busy Or ie.ReadyState <> 4: DoEvents:Wend

Excel VBA click website button with no name or ID that uses JSON

If only interested in that one tab I would first grab the parent element (div), which houses all the tabs, by its classname using a css class selector. I would then use a child combinator in combination with nth-child() selector to grab the second child div by its classname:

.querySelector(".table-view__tabs > div:nth-child(2)").Click 'Select tab direct

If potentially interested in all 3 tabs:

I would first grab the parent element (div), which houses all the tabs, by its classname using a css class selector. I would then use a child combinator to grab the child divs (which are the tabs) by their classnames. I would index into the returned nodeList to click the required tab:

Set tabs = .querySelectorAll(".table-view__tabs > div") 'select all tabs then index in for tab of choice
tabs.Item(1).Click

VBA:

Option Explicit

Public Sub ClickButton()

Dim ie As SHDocVw.InternetExplorer

Set ie = New SHDocVw.InternetExplorer

With ie
.Visible = True
.Navigate2 "https://fundcentres.lgim.com/uk/workplace-employee/fund-centre/#Product=WorkSave-Pension-Plan-(generation-3)"

While .Busy Or .ReadyState <> 4: DoEvents: Wend

With .Document

.querySelector(".table-view__tabs > div:nth-child(2)").Click 'Select tab direct

Stop 'Delete me later

Dim tabs As Object

Set tabs = .querySelectorAll(".table-view__tabs > div") 'select all tabs then index in for tab of choice
tabs.Item(1).Click '0 based so first tab is 0, second is 1 etc

Stop 'Delete me later

End With

Stop '<==Delete me later
.Quit
End With
End Sub

Reading:

  1. CSS selectors
  2. Child combinator
  3. Class selector
  4. :nth-child
  5. document.querySelectorAll
  6. document.querySelector

References (VBE>Tools>References):

  1. Microsoft Internet Controls

Excel VBA clicking button in internet explorer with unique element name

Try:

  If btn.GetAttribute("data-attribs-childassignmentid") = 665 Then

How to click a button in Internet Explorer using VBA

Your selector is wrong

The html is

<img style="CURSOR:HAND" src="http://www.bmf.com.br/bmfbovespa/images/comum/btoExcel.gif" align="absmiddle" hspace="0" onclick="salvaxls()">

You can use the following attribute = value selector

[onclick='salvaxls()']

You could also use $ ends with operator and target the src

[src$='btoExcel.gif']

Using a proper page load wait you have as follows

Option Explicit
'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub RetrieveInfo()
Dim ie As InternetExplorer
Set ie = New InternetExplorer

With ie
.Visible = True
.Navigate2 "http://www2.bmf.com.br/pages/portal/bmfbovespa/boletim1/TxRef1.asp"

While .Busy Or .readyState < 4: DoEvents: Wend

.document.querySelector("[src$='btoExcel.gif']").Click

Stop

End With
End Sub

There are lots of existing answers on SO regarding how to interact with Save/Open dialog. Personally, I prefer to automate with selenium basic and use Chrome to avoid this issue altogether

Excel VBA Internet explorer click button with no ID

Try

objie.document.forms(0).submit


Related Topics



Leave a reply



Submit