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?

1
  • 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. Commented Oct 11, 2012 at 17:20

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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...
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...

2 Comments

You are right, i tried this but it doesn't work for me. I think javascript runs after ReadyState is Complete.
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: stackoverflow.com/questions/153748/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.