How to fire WebBrowser RaiseEvent in console application c#?

109 views Asked by At

So basically I'm trying to fire WebBrowser's RaiseEvent from console applicaton but it does not work. In WinForms project it works well, so I'm just wondering what I have to do to make it work in console app. Is this even possible?

This is how I create thread for my WB

 private static Thread runBrowserThread(int mCandidatePosition)
        {
            var wbThread =  new Thread(() => {              
                mClient = new WebClient();
                mainWebBrowser.ScriptErrorsSuppressed = true;
                mainWebBrowser.AllowNavigation = true; 
                mainWebBrowser.DocumentCompleted += MainWebBrowser_DocumentCompleted;
                mainWebBrowser.Navigate(new Uri(xxx));
                Application.Run();
            });
            wbThread.SetApartmentState(ApartmentState.STA);
            return wbThread;
        }

and this is how I try to do it:

        HtmlElement selectType = gDoc.GetElementById("xxx");
        selectType.RaiseEvent("onchange");

The code above work well in WinForms project, but in console application nothing happens. Additionally, InvokeScript works well so it is a bit weird to me.

1

There are 1 answers

2
Ashkan Mobayen Khiabani On

You should use the InvokeMember() method of the HtmlElement:

 HtmlElement selectType = gDoc.GetElementById("xxx");
 selectType.InvokeMember("change");