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 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.
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 https://bananadesk.com/transactions/incoming
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
Next
Comments
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.