How to programmatically click on input element with class


Trying to programmatically click on an input type=submit button that is on a public website in code like below within other div class tags. I am using Visual Studio 2017 and C#. The part of the code on that web page that I am interested in is below and trying to click the "btn primary" input type.

         <div class="button_box_buttons">
            <input type="submit" class="btn primary" value="Sign In"
            onclick="return login_jsp.saveRemember && login_jsp.saveRemember() || true;" />

The code I have is something like below.

            oIEMain = new SHDocVw.InternetExplorer()
            Visible = true
            Log("Navigate to Homepage: " + sURL);
            HTMLDocument HTMLDoc = (HTMLDocument)oIEMain.Document;

            mshtml.HTMLElementCollection HtmlElementcol = (mshtml.HTMLElementCollection)HTMLDoc.getElementsByTagName("input");

           foreach (HtmlElement eleme in HtmlElementcol)
            //Check the attributtes you want
            if (eleme.GetAttribute("className") == "btn primary")
                //Check even the text if you want
                if (eleme.InnerText == "Sign In")
                    //Invoke your event

Running this code, I get an exception unhandled: 'mshtml.HTMLElementCollection'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{3050F56B-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'

How to get the button to click?


asked on Stack Overflow Nov 2, 2018 by BrainBusted

2 Answers


So the first thing I see is you need to wait for the HTML to download.

oIEMain.Navigate(sURL); does not run synchronously.

So anything that works with the HTML of the page.. that code needs to live in the oIEMain.DocumentCompleted event handler.

That may be enough to fix your error. You haven't provided enough information in your questions to know for sure.

answered on Stack Overflow Nov 2, 2018 by Sam Axe

Ended up resolving the issue. Below is associated code that got it to work. Hope it helps someone else.

        eContinue = (HTMLHtmlElement)HTMLDoc.getElementsByTagName("input").item(4);;
answered on Stack Overflow Nov 3, 2018 by BrainBusted

User contributions licensed under CC BY-SA 3.0