0

I use WebBrowser Control in my Web Application. On DocumentCompleted Event I want DocumentText in my string. My DocumentCompleted Event is like follow.

void IEBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    string str = IEBrowser.DocumentText;
}

Now the problem is DocumentText that I want I don't get it in DocumentCompleted Event. I think there is some javascript that do things after DocumentCompleted event. So I change my code something like this.

void IEBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    Thread th = new Thread(new ThreadStart(startthread));
    th.Start();              
}

public void startthread()
{
   //To Wait untill WebBrowser run that javascript
   Thread.Sleep(5000);
   string str = IEBrowser.DocumentText; 
}

Now with this code I am getting "Specified cast is not valid.". How can I make wait for the Thread in which WebBrowser is running?

Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
Hiren
  • 1,381
  • 5
  • 24
  • 41
  • 1
    use web browse control in web application? try to scrape something within web application? a better way is to use httpwebrequest, or even webclient. – urlreader Oct 11 '12 at 17:20

2 Answers2

1

although I do not think it is a good way to do this, one way to do it is: do not process this in DocumentComplete. Do something like this:

  1. do whatever you need to do ... then at a point, the browser needs to navigate to a url, then (you may need check syntax, just type out of my header):

                IEBrowser.Navigate(url);
            bool flag = true;
            int times = 0;
            while (flag)
            {
                Sleep(500);
                Application.DoEvent();
    
                if (IEBrowser.ReadyState == WebBrowserReadyState.Complete)
                {
                    times ++;
                }
                else
                {
                    times = 0;
                }
    
                if (times >= 10)
                {
                    flag = false;
                }
            }
    
            string str = IEBrowser.DocumentText;
    

Again, this is just a work around for you to try. depends on your purpose, this can be improved a lot. but I used the procedure similar to this for many scraping work, and it works fine. Depends on the page, the documentComplete may be fired multiple times.

good luck

urlreader
  • 6,319
  • 7
  • 57
  • 91
  • Thanks a lot ,it works exactly how I want. I created my code now. I need to finalize my code so want some extra help. You put Sleep() in your code. This is same as Thread.Sleep();. Because if it is Thread.Sleep() it will stop execution so I don't include it. – Hiren Oct 11 '12 at 18:52
  • I would put this in a TimeCallback instead, that way you are still checking it in intervals, you can still stop it, and it won't kill your main thread... – emalamisura Oct 12 '12 at 15:47
0

I am not quite sure what you are doing here but I can assure you kicking off that thread is not the correct solution.

Try this, since IEBrowser_DocumentCompleted should be called a couple times in my experience...

void IEBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {     
if (IEBrowser.ReadyState = WebBrowserReadyState.Complete)         
     {
       //Do my stuff
     }
}

Not sure if this will work for you but I have had success in past with it...

emalamisura
  • 2,816
  • 2
  • 29
  • 34
  • You are right, i tried this but it doesn't work for me. I think javascript runs after ReadyState is Complete. – Hiren Oct 11 '12 at 17:20
  • If you want to try to wait 5 seconds use a TimerCallback, but I wouldn't rely on that for any kind of production solution as its undeterministic. You can also Inject script into the page, and you can use this to determine if the Javascript you are waiting for has completed or not as well...Here are details on Script injection and even getting a result back from it: http://stackoverflow.com/questions/153748/how-to-inject-javascript-in-webbrowser-control – emalamisura Oct 11 '12 at 17:23