How can I loop webbrowser control?
I want my program to go to a site, fill in the data, click submit then once completed go to the next site.
I know you can do this in webrequest but I can only get it to login but once Ive logged in I dont know how to make it click a button automatically, I only know how to do it semi-automatically by getting the ID.
Re: How can I loop webbrowser control?
There's no actual loop. You call Navigate and handle the DocumentCompleted event. When the document has loaded, you process it and then call Navigate again.
Re: How can I loop webbrowser control?
how do i use the document completed thing? can you use it to make one button perform several actions for example log into a website,document completed fires, then navigate to a form, then fill form, then document completed, then navigate to next site?
Re: How can I loop webbrowser control?
E.g.
vb.net Code:
Public Class Form1
Private urls As String()
Private urlIndex As Integer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.urlIndex = 0
Me.urls = {"www.somewhere.com",
"www.anywhere.com",
"www.nowhere.com"}
'Load the first page.
NavigateToNextUrl()
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
'Process the page just loaded here.
'Load the next page.
NavigateToNextUrl()
End If
End Sub
Private Sub NavigateToNextUrl()
'Continue only if there is a page pending.
If Me.urlIndex < Me.urls.Length Then
WebBrowser1.Navigate(Me.urls(urlIndex))
Me.urlIndex += 1
End If
End Sub
End Class
Re: How can I loop webbrowser control?
Thanks for the code, it really gave me a ton of ideas. Another question though, I am trying to make the bot login once it has reached the page, here is the code I am using to try to do so:
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
'Process the page just loaded here.
WebBrowser1.Document.GetElementById("emailaddress").SetAttribute("value", "
[email protected]")
WebBrowser1.Document.GetElementById("password").SetAttribute("value", "hawk411")
WebBrowser1.Document.GetElementById("action").InvokeMember("submit")
End If
But when I run it iI get an error, something about a null reference. Can you help?
Re: How can I loop webbrowser control?
When a NullReferenceException is thrown, the first thing to do is test all the references on the offending line and determine which one is Nothing. I would guess that one of your GetElementById calls is not finding an element with the specified ID so it's returning Nothing. You can't call SetAttribute or InvokeMember on an element that doesn't exist.
Re: How can I loop webbrowser control?
Oh alright thanks. If I need anymore help I will reply here, anyways thanks for everything
Re: How can I loop webbrowser control?
In this part of the code:
vb Code:
Me.urlIndex = 0
Me.urls = {"www.somewhere.com",
"www.anywhere.com",
"www.nowhere.com"}
Instead of typing all the urls, can I tell it to visit all the sites in CheckedListBox1? I tried editing the code to fit my means but I kept getting errors
Re: How can I loop webbrowser control?
'urls' is simply a String array used for the purposes of the example. You can use any list you like, including the CheckedItems of a CheckedListBox.
That said, it would be possible for the CheckedItems to change during the process, so that wouldn't be a good idea. It would be better to copy the contents of the CheckedItems to another list that won;t change and then use that as the source for the process. It just so happens that, like all collections, the CheckedItems has a CopyTo method that will copy its contents to an existing array. You might also use a bit of LINQ to create an array from the collection.
Re: How can I loop webbrowser control?
Another way could be to use a Timer (with 1 sec interval or so), instead of the DocumentCompleted event.
The advantage you would get over this method is that you would be able to put a check on maximum time you want to wait for the webpage to load. So say, if one of the urls is taking exceptionally long time to load, you can log it somewhere (or do whatever you want to with it), and continue on to the next one.
JM has already shown all what you need to do. The only change you would need is to put a Timer, set its Interval property and move the code inside the WebBrowser DocumentCompleted event into the Timer Tick event. Then you can add additional checks there if you want to.
You should also disable the timer once you are done checking all the urls.
Re: How can I loop webbrowser control?
@jmcilhinney Can you give me some sample code? Cause I tried replacing the urls and just wrote 'CheckedListBox' but that didnt work
@Pradeep Thanks for the timer idea
Re: How can I loop webbrowser control?
Quote:
Originally Posted by
kytro360
Can you give me some sample code? Cause I tried replacing the urls and just wrote 'CheckedListBox' but that didnt work
I'm not quite sure what would make you think that that would work when I specifically said:
Quote:
Originally Posted by
jmcilhinney
'urls' is simply a String array used for the purposes of the example. You can use any list you like, including the CheckedItems of a CheckedListBox.
I suggest that you start by reading the documentation for the CheckedListBox class, so that you understand how it works.