Reputation: 5419
I'm trying to use this snippet to test of an element has a specific text.
HtmlDocument element = webBrowser2.Document;
if (element.GetElementById("gbqfsa").InnerText == "Google Search")
{
HasSucceeded = 1;
}
return HasSucceeded;
However the first line throws the exception "Specified cast is not valid." What am I doing wrong?
Upvotes: 0
Views: 1156
Reputation: 1
I encounter this problem when return HtmlDocument as property from my custom user control. (Which embedded WebBrowser control)
Cause of error because access document from other thread.
/// <summary>
/// Error version '
/// </summary>
public HtmlDocument Document
{
get
{
// Throw error 'Specified cast is not valid'
return this.webBrowserMain.Document;
}
}
But I don't known why error is not 'CrossThread Operation access ...' but next code solved my problem
/// <summary>
/// Fixed version
/// </summary>
delegate HtmlDocument DlgGetDocumentFunc();
public HtmlDocument GetDocument()
{
if(InvokeRequired)
{
return (HtmlDocument)this.webBrowserMain.Invoke(new DlgGetDocumentFunc(GetDocument), new object[] { });
}
else
{
return this.webBrowserMain.Document;
}
}
Upvotes: 0
Reputation: 85096
Is it possible that you are using the wrong HtmlDocument class? WebBrowser.Document is of the type:
System.Windows.Forms.HtmlDocument
But I noticed that there is also another possible namespace:
System.Windows.Browser.HtmlDocument
I would check to make sure the namespace you included was System.Windows.Forms.HtmlDocument
Upvotes: 2