NEO
NEO

Reputation: 23

how to click a button on webpage having class name using vba excel

I am a rookie in VBA excel. There is a web page application in which i need to click a button, the source of which is

<em class="x-btn-arow" unselectable="on">
<button class= x-btn-text" id="ext-gen7576" style=""   type="button">Actions</button>

Sub xx()
Dim IE As Object
Dim doc As HTMLDocument
Dim l As IHTMLElement
Dim lo As IHTMLElementCollection




Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://theapplicationlink"
Do
DoEvents
Loop Until IE.ReadyState = 4



Set doc = IE.Document
Set lo = doc.getElementsByTagName("button")
For Each l In lo
If l.getAttribute("class") = "x-btn-text" Then
l.click



End If
Next

End Sub

it doesn't throw any error but it doesn't click the button.

I cannot use ID as it keeps on changing each time i launch the application. Also the class and type is same for other buttons also.

Forgive me for any technical errors Any help will be a huge favour here.

Upvotes: 1

Views: 11894

Answers (2)

QHarr
QHarr

Reputation: 84465

There is an id. Does it change completely or does part of it remain the same? If it were you could partial match on the bit that remain the same using a CSS selector.

That aside you could use:

objIE.document.querySelector("button[class*= x-btn-text]").Click

This uses a CSS selector to target the element of button[class*= x-btn-text]. Which will be the first element with button tag having attribute class with value containing x-btn-text.

Upvotes: 1

RealLifePM
RealLifePM

Reputation: 60

"button" is not a HTML tag. use a "Tag". let me give you an example here. Replace the "strTagName" with a HTML tage that inlcudes the thing you want to click.

Dim objTag As Object
For Each objTag In objIE.document.getElementsByTagName(strTagName)
    If InStr(objTag.outerHTML, "x-btn-text") > 0 Then

        objTag.Click
        Exit For

    End If
Next

Upvotes: 0

Related Questions