I’m trying to make a simple program that goes to a page and clicks on a button. However for some reason when I’d try to call a .GetElementByID on a WebBrowser.Document there would be none available, there’s only stuff like Equals, GetHash or what have you. No GetElementByID. So I tried to do this:
private void button1_Click(object sender, RoutedEventArgs e)
{
webBrowser1.Navigate("http://kdp.amazon.com");
HtmlDocument doc = (HtmlDocument)webBrowser1.Document;
doc.GetElementByID("whatever");
}
and I get this error:
Unable to cast COM object of type ‘mshtml.HTMLDocumentClass’ to class
type ‘System.Windows.Forms.HtmlDocument’. Instances of types that
represent COM components cannot be cast to types that do not represent
COM components; however they can be cast to interfaces as long as the
underlying COM component supports QueryInterface calls for the IID of
the interface.
Am I missing a reference or something? All of the tutorials I found on stackoverflow say to call methods on webBrowser.Document that I don’t have available.
It sounds like you’re using the unmanaged WebBrowser ActiveX control instead of the managed wrapper.
Change the control to the managed
System.Windows.Forms.WebBrowser.