2

I'm using this code which is meant to check the text in webBrowser1, although instead I'm getting the error "Specified cast is not valid." for string docText = webBrowser1.Document.Body.InnerText;. Any ideas why? Could it be because I'm accessing the webBrowser from another thread? Thanks.

public Form1()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    string docText = webBrowser1.Document.Body.InnerText;

    if (docText == "Hello")
    {
        MessageBox.Show("Alerted!");
    }
}

private void timer1_Tick(object sender, EventArgs e)
{
    backgroundWorker1.RunWorkerAsync();
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Joey Morani
  • 25,431
  • 32
  • 84
  • 131
  • 1
    Can you post the stack trace for the exception? – Fredrik Mörk Mar 05 '12 at 12:12
  • 2
    According to [the MSDN](http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser.document.aspx) the `Document` property is an `object` so you'd need to cast it before dereferencing it to get `Body.InnerText`. Have you missed out some code? – ChrisF Mar 05 '12 at 12:12
  • @ChrisF shouldn't that make the compiler complain, rather than causing an exception in when running the code? – Fredrik Mörk Mar 05 '12 at 12:21
  • @FredrikMörk - yes, which is why I asked if the OP had missed some code from his question. – ChrisF Mar 05 '12 at 12:22

4 Answers4

5

The exception may in fact be caused by accessing the WebBrowser.Document property from a thread that isn't the main UI thread. You can verify that by looking for the following lines in the stack trace of the System.InvalidCastException:

at System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetLocation()
at System.Windows.Forms.WebBrowser.get_Document()

If this is the case, try passing the content of the web page to the background thread as an argument instead:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    var docText = (string)e.Argument;
}

private void timer1_Tick(object sender, EventArgs e)
{
    var docText = webBrowser1.Document.Body.InnerText;
    backgroundWorker1.RunWorkerAsync(docText);
}
Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
2

I will try with...

backgroundWorker1.RunWorkerAsync(webBrowser1.Document.Body.InnerText);

this will remove the cast exception

and in DoWork

string docText = e.Argument.ToString();

this will remove the UI thread issue

Steve
  • 213,761
  • 22
  • 232
  • 286
0

Maybe you should wait for DocumentCompleted event from WebBrowser ctrl before accessing Document.

ebutusov
  • 563
  • 2
  • 5
0

The .Body section will return an object null reference exception and .innerHTML will not be recognized as a string if you haven't waited for the page to load, it may be triggering that error you are getting as a result. have you done correct waiting for document to load before all this? check out my answers on how to do proper waiting on webbrowser control if you need help with this.

Erx_VB.NExT.Coder
  • 4,838
  • 10
  • 56
  • 92