VBA "IWebBrowser 2 Failed" on button click

Darkcloud617

New Member
Joined
Sep 7, 2017
Messages
38
I have a code that logs in to a website as it should but when it goes to click into a drop down menu it errors "Method 'Document' of object 'IWebBrowser 2' failed" and errors on this line
VBA Code:
        IE.Document.getElementsByClassName("MenuBar_Menu")(1).Click

Here is the full code:
VBA Code:
Sub test()

Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")

    With CreateObject("InternetExplorer.Application")
        Dim chng As Object, chk As Object
        .Visible = True
        .Navigate "https://www.google.com"

        While .Busy Or .ReadyState <> 4: Application.Wait (Now + TimeSerial(0, 0, 1)): Wend

        Set chng = .Document.createEvent("HTMLEvents")
        chng.initEvent "change", True, False

        .Document.getElementById("txtEmail").Value = "XXXX"
        .Document.getElementById("txtPassword").Value = "XXXXX"


        .Document.getElementById("Main_Main_btnSignIn").Click
        
        Application.Wait (Now + TimeValue("0:00:10"))
        
        IE.Document.getElementsByClassName("MenuBar_Menu")(1).Click
        
    End With
    
End Sub

This is a snip of the website code that contains the menu I am trying to click:
Element.PNG

I am trying to get VBA to click the last option "All Open Orders in view" but I think I'm getting confused because its a hover drop down menu. When hovering over the box on the website the javascript code changes to "MenuBar_Menu_Item Menu_Item_Hover" from "MenuBar_Menu_Item". I'm not sure how to account for that.

Any help would be greatly appreciated. Thank you.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Maybe go for it's ID instead of the Class
Hard to guess without being able to try
 
Upvote 0
IE.Document.getElementsByClassName("MenuBar_Menu")(1).Click
IE here doesn't reference a web site because of:

VBA Code:
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")

    With CreateObject("InternetExplorer.Application")
The fix is to change the line to:
VBA Code:
        .Document.getElementsByClassName("MenuBar_Menu")(1).Click
and delete the Set IE = .... line.

Also note that (1) references the 2nd element with the "MenuBar_Menu" class name. Use (0) for the 1st element.
 
Upvote 0
Solution
Thank you, that did correct the error. I still have the original issue but I will post that in another thread. Thank you so much for the assistance.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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