Click img within HTML table with changing td id via VBA

watkinson

New Member
Joined
Feb 11, 2020
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I am trying to click a little plus sign to expand a selections menu that appears after login as part of an IE automation. I have tried every click in the book, hopefully besides the one y'all have. Here's the code I've used to get into the website, as well as a picture of the HTML when already logged in. the highlighted img is what I'm trying to click. The main issue is that the td id "ext-element-6" changes every time I login. I've seen it as "ext-element-6" all the way up to "ext-element-50". Thanks!

VBA Code:
Sub Help()

Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLDoc2 As MSHTML.HTMLDocument
Dim HTMLInput As MSHTML.HTMLDocument
Dim Buttons As MSHTML.IHTMLElementCollection
Dim Button As MSHTML.IHTMLElement
Dim Bs As MSHTML.IHTMLElementCollection
Dim B As MSHTML.IHTMLElement
Dim Start_Date As Integer
Dim End_Date As Integer
Dim rNum As Integer
Dim cNum As Integer
Dim tRows As Object
Dim temp As Object
Dim Table As Object
Dim tHead As Object
Dim tCells As Object
Dim np As Variant
Dim numPages As String
Dim elements As Object
Dim element As Object
Dim doc As MSHTML.HTMLDocument
Dim img As MSHTML.HTMLImg

    IE.Visible = True
    IE.Navigate "www.google.com"
        
        Do While IE.Busy: DoEvents: Loop
        Do Until IE.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
        
    Set HTMLDoc = IE.Document
                                                            
        HTMLDoc.forms("frmLogin").elements("userLogin").Value = "testu"
        HTMLDoc.forms("frmLogin").elements("userPassword").Value = "testp"


        HTMLDoc.getElementById("loginButtonBg").Click
        

        'please help here

        
        
End Sub

help.PNG
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi,

I don't do anything with IE automation and no way to test this without an example link so I don't know if this will work but it might set you off in the right direction.
It may also be possible to use a loop x 1 to 100 for example) to try opening it ("ext-element-" & x) in a loop if it is the only clickable link with said id

Code:
Set Link = ie.document.getElementsByTagName("td")
    For Each l In Link
        If Link.id Like "ext-element-##" Then
            Link.Click
            Exit For
        End If
    Next l
 
Last edited:
Upvote 0
Your third line has a run-time error, I believe perhaps because id is autocapitalizing. "Dim ID as" does not exist in my code, so it must be trying to return something else.

I changed it from two #s to one to see if it would capture a single digit and it didn't. Source code was element-7 that time. Thank you for the idea though!
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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