简体   繁体   中英

How to find a button on webpage

I am trying to perform multiples actions on a webpage to help users set up their preferences. I am using Excel VBA as it is the only way in the company to automate tasks.

First, they have to login, an operation which I was able to perform. Then, I browse to the appropriate page.

The problem happens here: with Internet Explorer if I right-click the page and display source code, only a part of the page is shown. Also, depending on the area on which I perform this action, source code is different, it is not a single piece.
If I use the DOM Explorer, things are different. The full page code is displayed and I can point on the button I want to click.

Button code:

<a href="javascript:somewords('somelink',var1,var2,false)">Click here !</a>

My code:

Sub Connect_to_system()

    Set IE = New InternetExplorerMedium
    Dim IEDoc As HTMLDocument
    Dim htmlTagCol As IHTMLElementCollection
    Dim Generic As HTMLGenericElement
    
    With IE
        .Visible = True
        .navigate "http://example.com/login?username=johndoe&password=123"

        Do Until .readyState = 4
            DoEvents
        Loop
        
        .navigate "http://example.com/somepage.jsp?reference123"
        
        Do Until .readyState = 4
            DoEvents
        Loop
        
        Application.Wait (Now + TimeValue("0:00:10"))
        
        Set tags = IE.document.getElementsByTagName("a")
        For Each tagx In tags
            If tagx.href = "javascript:somewords('somelink',var1,var2,false)" Then
                tagx.Click
            End If
        Next
    
    End With
End Sub

My code loops three times (so I understand that it detects tags) but none of them matches my condition. So it gets out of the loop and nothing happens.
I tried to see what the tags detected and it seems that only the top ribbon of the page is recognized / loaded.
Even with the InnerText function these methods didn't work.

With your minimally supplied html code, this will have to do.

You can use innerHTML to compare your string value on the elements with your tag name of a .

Sub ieBusy(ie As InternetExplorerMedium)

    Do While ie.Busy Or ie.readyState < 4
        DoEvents
    Loop

End Sub

Sub Connect_to_system()

    Dim ie As New InternetExplorerMedium, aElemColl As Object, aElem As Object

    With ie

        .Visible = True
        .navigate "http://example.com/login?username=johndoe&password=123"
        ieBusy ie
        .navigate "http://example.com/somepage.jsp?reference123"
        ieBusy ie
        Set aElemColl = .document.getElementsByTagName("a")

    End With

    For Each aElem In aElemColl

        If aElem.innerHTML Like "*javascript:somewords('somelink',var1,var2,false)*" Then
            aElem.Click
            Exit For
        End If

    Next

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM