DocumentCompleted fires twice?
Hi All,
I am trying to wait for the webbrowsers page to have finished loading to then carry out the next part of my code.
However i am having problems because the DocumentCompleted event fires twice?
I was just wondering why this is happening and if there's anyway to ignore the first event?
Many thanks,
Chloe ~X~
Re: DocumentCompleted fires twice?
vb Code:
WebBrowser1.Navigate("http://www.vbforums.com/showthread.php?t=605906")
While Not WebBrowser1.ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
End While
MsgBox("loaded")
Re: DocumentCompleted fires twice?
I have to disagree with Paul's suggestion. "Busy waiting" loops like that are never a good idea. If the DocumentCompleted event is raised twice it's because two documents are loaded. Most likely there's a frame in the main page. You can still test the ReadyState property but you do it in the DocumentCompleted event handler. It will be Complete the second time it's raised.
Re: DocumentCompleted fires twice?
Hi guys,
I tried Paul's method and it didn't resolve the issue i'm having, it made the program freeze for a long time.
This is my code, can you see any problems with it?:
vb Code:
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
If browserPage.Contains("Recruitment/AdManager/JobList.aspx") Then
For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input")
If input.GetAttribute("id") = "rptResults_ctl01_chkAction" Then
input.SetAttribute("checked", "true")
End If
If input.GetAttribute("name") = "btnCopyJob" Then
input.InvokeMember("click")
End If
Next
End If
End If
End Sub
I want to be able to wait till the second event to then run my code, at the moment it is trying to run my code on the first and second dcoumentCompleted events?
Many thanks,
Chloe ~X~
Re: DocumentCompleted fires twice?
Re: DocumentCompleted fires twice?
Hi Paul,
What i want to happen is that when that page loads, the checkbox is checked and then a button is clicked.
At the moment my code is trying to tick the checkbox before the page is loaded and so i get an error from the page in a pop up saying i ahve not ticked the checkbox. If i then click ok on the msgbox the code then ticks the check box correctly and then clicks my button as it should? I just need it to wait a little longer before executing?
Thanks,
Chloe ~X~
Re: DocumentCompleted fires twice?
Is the WebBrowser control ever used by the User? or are you just using its methods to login in a site?
If the latter is true, then I would suggest ditching the WebBrowser control altogether and just use HTTPWebRequest.
You can accomplish the same things without the overhead of the WebBrowser control.
Re: DocumentCompleted fires twice?
Hi,
Yes the webbrowser is being used by the user, the idea is i'm automating part of the task the user has to do manually?
Thanks,
Chloe ~X~
Re: DocumentCompleted fires twice?
Turns out it was my code :S!
When i seperated the action like below it worked fine:
vb Code:
For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input")
If input.GetAttribute("id") = "rptResults_ctl01_chkAction" Then
input.SetAttribute("checked", "true")
End If
Next
For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input")
If input.GetAttribute("name") = "btnCopyJob" Then
input.InvokeMember("click")
End If
Next
Thanks as always for your responses.
Chloe ~X~
Re: DocumentCompleted fires twice?
Quote:
Originally Posted by
jmcilhinney
I have to disagree with Paul's suggestion. "Busy waiting" loops like that are never a good idea. If the DocumentCompleted event is raised twice it's because two documents are loaded. Most likely there's a frame in the main page. You can still test the ReadyState property but you do it in the DocumentCompleted event handler. It will be Complete the second time it's raised.
Just wanted to comment -- I found this solution via Google and it fixed a problem I've been having wit VB for ages. Thanks, jmcilhinney!