How can i loop this code in visual basic?
im making a friend adder for teenspot and this is what i have. basically you click a button and it adds the person selected on the list, then you have to click it again to add the next one. the thing is i need to know how to loops this so it just keeps moving down the list adding people automatically
WebBrowser1.Navigate("www.teenspot.com/profiles/" & ListBox.SelectedItem & "/add")
ListBox.SelectedIndex = ListBox.SelectedIndex + 2
' This is the "sleep" function.
' This makes your webbrowser finish loading before new action.
Do Until WebBrowser1.ReadyState = WebBrowserReadyState.Complete And WebBrowser1.IsBusy = False
Application.DoEvents()
Loop
WebBrowser1.Document.GetElementById("confirm").InvokeMember("click")
ive tried multiple times with do...loop and for..next and all that kinda stuf开发者_运维知识库f, but i still havent figured it out. what it does is, it only runs through the add me pages and never clicks confirm which is the second part. im kinda new to vb so if someone can i help id really appreciate it
To answer this:
the thing is i need to know how to loops this so it just keeps moving down the list adding people automatically
To add all friends in the list with one button click:
Use For Each/ Next
For Each item As String In ListBox1.Items
WebBrowser1.Navigate("www.teenspot.com/profiles/" & item & "/add")
Do Until WebBrowser1.ReadyState = Complete And WebBrowser1.IsBusy=False
Application.DoEvents()
Loop
WebBrowser1.Document.GetElementById("confirm").InvokeMember("click")
Next
To add only the selected friend and then move the selected listitem one step down to wait for a new button click, do this:
If Not ListBox1.SelectedItem Is Nothing Then
WebBrowser1.Navigate("www.teenspot.com/profiles/" & ListBox1.SelectedItem.ToString & "/add")
If Not ListBox1.SelectedIndex = ListBox1.Items.Count - 1 Then ListBox1.SelectedIndex += 1
Do Until WebBrowser1.ReadyState = Complete And WebBrowser1.IsBusy=False
Application.DoEvents()
Loop
WebBrowser1.Document.GetElementById("confirm").InvokeMember("click")
End If
And for this:
it only runs through the add me pages and never clicks confirm which is the second part.
We have to know how the HTML-document is made up to research this, has the element "confirm" a javascript-click-event or is it just a post-form-button? I cant check this out without be logged in to the site. (And Im not a teenager so I will not create an account there) ;-)
But, you should be able to perform the click without InvokeMember I think. Like this:
WebBrowser1.Document.GetElementById("confirm").click()
I'm not 100% sure, but I think I've done it like that before. If the button has a javascript like click="AddFriend()" or something like that, maybe you could use the invokescript to call that function directly instead of clicking the button, like this:
WebBrowser1.Document.InvokeScript("AddFriend()")
精彩评论