Solution 1 :

When using VBA script to automate IE browser, the getElementsByClassName() method returns an array-like object of all child elements which have all of the given class name(s). So, we should find the special element according the array index.

Sample code as below

Public Sub ClickTest()

    Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .Navigate2 "<web site url>"

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

        ie.document.getElementsByClassName("button -tertiary")(0).Click
    End With
End Sub

The web page resource like this:

  <a class="button -tertiary" href="javascript:alert('hello')">Exportar Todo</a>

The above sample works well on my side (using IE 11), please check it and make sure we could use VBA to find the link in IE browser.

Then, change the javascript alert function to your export_all_to_excel() function, and check the code in the export_all_to_excel() function, make sure they all support IE browser.

Besides, here is another thread about Clicking on hyperlink with partial href on Internet Explorer using vba, you could refer it.

Solution 2 :

At the end I decided to use Selenium and Chrome to click the button and now it works! Thanks for your help guys

Problem :

The VBA code is not responsible because it doesn’t work on IE 11 even not programmatically.

The button works with Chrome or Firefox but I cannot manage to use Selenium on my computer, it is throwing errors.

I asked the coding team of the website to tell me more precisely how this button was created on this link (Exportar todo) on this link

Screen capture of the website:
Screen capture of the website

<a class="button -tertiary" href="javascript:export_all_to_excel()">Exportar Todo</a>

If I inspect the html page, I see stuff like this:

o=null,i=null,a=/Version/(S+)s+Safari/;if(navigator.userAgent){var s=navigator.userAgent,c=s.match(a);c&&s.indexOf("Chrome")===-1&&s.indexOf("Chromium")===-1&&(o="Safari",i=c[1])}n.exports={agent:o,version:i,match:r}},{}],21:[function(t,n,e){function r(t,n){Var... 

Probably because their website is only supported on Chrome and Safari.

Here is the VBA code, it won’t click the button Exportar todo even though I am entering the If block properly:

For Each MyHTML_Element In HTMLDoc.getElementsByClassName("button -tertiary")
Debug.Print "button found"
MyHTML_Element.Click: Exit For


Comment posted by Stavros Jon

Could you provide a longer snippet of the button’s html? Is it an actual button element? Because all I see is an anchor element. A URL would also be helpful, unless it’s confidential.

Comment posted by Zwenn

I can’t see a button, only a link. Try this:

Comment posted by Pierre Qui Roule

@StavrosJon I updated the post with an image capture and a link, thanks!

Comment posted by Pierre Qui Roule

@Zwenn Thank you! I will try this but I suspect it won’t work because they restrict the browser types in the code

Comment posted by Zwenn

Which browser types? IE 11 is IE 11. And IE 11 is part of your topic. Apart of that … A click on a link is a click on a link. That’s no rocket science.

Comment posted by Pierre Qui Roule

@zhilvmsft Thank you very much for your detailed answer!

Comment posted by Pierre Qui Roule

@zwenn suggested the same code in his comment but it doesn´t click on the button. Or maybe it does but nothing is happening afterwards because the website is probably filtering the supported browsers and then not running the JS function.

Comment posted by Zhi Lv

You could add a debugger (or using the console.log() function) in the export_all_to_excel() function, to check whether this function is executed or not. Besides, when execute the VBA script, is there any error ? You could also use F12 developer tools to check the VBA opened IE window to check is there any error.