I'm using visual Basics 2008 Express, I have this code on the "Go" Button of My mainform. The program shows this error: "Object reference not set to an instance of an object." Here is the code I'm trying to get to work.The code stops on the line that is underlined. I really need help getting my app to Log in. (Hell I can't get it to fill the text boxes)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click WebBrowser1.Navigate(ComboBox1.Text) End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted WebBrowser1.Document.GetElementById("emailAddress").SetAttribute("type", login.Text) WebBrowser1.Document.GetElementById("pswd").SetAttribute("type", login.Text) WebBrowser1.Document.GetElementById("btn_login").InvokeMember("click") End Sub
It will wait for the webbrowser to fully loaded before it send all those value.
Thanks for your Time Waldo. Ok I tried the code under webbrowser1.Documents Complete. and No Dice. It navigates to the page then It just sits there. Could it be that the constantly changing Notice in the center of the page is not allowing the page to become inactive? Anyway Thanks for the help I sure need it. Oh this time it didn't flag any lines of code.Also I am running in debug mode in cast that may have something to do with it.
Waldo, I have tried all my reasonable options. I even tried this:
HTML Code:
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
WebBrowser1.Navigate(ComboBox1.Text)
WebBrowser1.Document.GetElementById("emailAddress").SetAttribute("type", login.Text)
WebBrowser1.Document.GetElementById("pswd").SetAttribute("type", login.Text)
WebBrowser1.Document.GetElementById("btn_login").InvokeMember("click")
End Sub
That causes quite a disturbance when it navigated to the website. Every thing started flickering and flashing But it still wouldn't fill those textboxes with the proper content.Also I found out that the Id=emailAddress should be Id=emailAddressLocal As seen In this source code of the login Box.
3. To send click is a little bit tricky as they use image as a button. But here you go:
PHP Code:
'collect all <img> tag name Dim elemCollec As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img") 'iterate each <img> found to look for unique string of the button For Each tagname As HtmlElement In elemCollec Dim ctrlname As String = tagname.GetAttribute("src").ToString 'get atribute with src If ctrlname.Equals("http://www.swagbucks.com/content/swagbucks-com/images/login_button.jpg") Then 'match with the buttin image tagname.InvokeMember("click") invoke click End If Next
The code above should work. Tell me if you need further help.
Private Sub lblPass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub btnGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGet.Click
login.Show()
End Sub
Private Sub txtUser_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub SwaggerLee1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebBrowser1.Navigate(ComboBox1.Text)
End Sub
Private Sub lblUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblUser.Click
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
WebBrowser1.Document.GetElementById("emailAddressLocal").SetAttribute("value", lblUser.Text)
WebBrowser1.Document.GetElementById("pswdLocal").SetAttribute("value", lblPass.Text)
WebBrowser1.Document.GetElementById("btn_login").InvokeMember("click")
'collect all <img> tag name
Dim elemCollec As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")
'iterate each <img> found to look for unique string of the button
For Each tagname As HtmlElement In elemCollec
Dim ctrlname As String = tagname.GetAttribute("src").ToString 'get atribute with src
If ctrlname.Equals("http://www.swagbucks.com/content/swagbucks-com/images/login_button.jpg") Then 'match with the buttin image
tagname.InvokeMember("click")
End If
Next
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
End Sub
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
End Sub
End Class
I also tried ti add webbrowser1.Navigate.(signInBoxLocal) Between the "document complete" and the "webbrowser Get elements" commands. This intered the signinBoxLocal text into Swaggs search and it did a search for "signinBoxLocal" but still no login. Notice that I don't have TextBox1 and TextBox2 in my code because They haven't been declared. My lblUser = My.Username in my login text file.
After I clicked button1, it navigate to the URL, and after finished loading, it send the username and password from the lblUser.Txt and lblPass.Txt. Then it click the login button.
Here is the full code:
PHP Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click WebBrowser1.Navigate(ComboBox1.Text) End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted WebBrowser1.Document.GetElementById("emailAddressLocal").SetAttribute("value", lblUser.Text) WebBrowser1.Document.GetElementById("pswdLocal").SetAttribute("value", lblPass.Text) 'WebBrowser1.Document.GetElementById("btn_login").InvokeMember("click") 'collect all <img> tag name Dim elemCollec As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img") 'iterate each <img> found to look for unique string of the button For Each tagname As HtmlElement In elemCollec Dim ctrlname As String = tagname.GetAttribute("src").ToString 'get atribute with src If ctrlname.Equals("http://www.swagbucks.com/content/swagbucks-com/images/login_button.jpg") Then match with the buttin image tagname.InvokeMember("click") End If Next End Sub
Ok I had to do a lot of finagling To get the code to this point. Surprisingly it was Your (waldo) button click code that helped me get both username and password to load into the site's text boxes. But I lack the code to click the login Button. any way here is the code so far.
HTML Code:
Private username, password As String
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Login(username, password)
End Sub
Private Sub Login(ByVal username As String, ByVal password As String)
Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("Input")
For Each curElement As HtmlElement In theElementCollection
Dim controlName As String = curElement.GetAttribute("id").ToString
If controlName = "emailAddressLocal" Then
curElement.SetAttribute("Value", lblUser.Text)
ElseIf controlName = "pswdLocal" Then
curElement.SetAttribute("Value", lblPass.Text)
End If
Next
End Sub
Is there any way to add another "If" for the invoke click?
Is there any way to add another "If" for the invoke click?
Do you meant this?
PHP Code:
Dim elemCollec As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img") 'iterate each <img> found to look for unique string of the button For Each tagname As HtmlElement In elemCollec Dim ctrlname As String = tagname.GetAttribute("src").ToString 'get atribute with src If ctrlname.Equals("http://www.swagbucks.com/content/swagbucks-com/images/login_button.jpg") Then match with the button image tagname.InvokeMember("click") End If Next
That code search for <img> tag and the loop will look for the image source and when it found, it invokes the click.
__________________
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
What is "scr" when I tried the code as you posted it kept opening up 3 more windows to login to twitter, facebook and Yahoo Search. Then It would ask me my login info and start hitting me with several error messages. but I could tell that the button click was happening only repeatedly.Causing the windows that I closed to reopean. Anyway I think that your button click code has to be modified to get it to work. I'm gettin excited cause I can now see my username and pass in the boxes I just gotta figure how to send that click to the Button. Thanks So much for your helping me with this!
Here is the code that I'm trying to get to click the login Button. I've tried everything, That I can think of. I sure can use some help! There is a btn1 btnLogin that I've also tried from the source code but still no dice. Please HELP!
HTML Code:
For Each curElement As HtmlElement In theWElementCollection
Dim controlName As String = curElement.GetAttribute(" tagname").ToString
If controlName = "frmLogIn-tbar" Then
WebBrowser1.Document.GetElementById("btn1 btnlogin").InvokeMember("click")
End If
Next
System.Threading.Thread.Sleep(1000)
With that post I was able to auto logon to swagbucks only problem is when I get to the successful logon page I get a ton of java script errors making it practically useless.
I finally have a auto logger! The solution was a simple one line of code added to the bottom of:
F
HTML Code:
or Each curElement As HtmlElement In theElementCollection
Dim controlName As String = curElement.GetAttribute("id").ToString
If controlName = "emailAddressLocal" Then
curElement.SetAttribute("Value", lblUser.Text)
ElseIf controlName = "pswdLocal" Then
curElement.SetAttribute("Value", lblPass.Text)
End If
Next
No src image callin or href It is so simple even Ghst can do it!
CONGRAT!!!!! I know how frustrating the whole trial and error programming can be. And thanks for sharing the code also.
Just out of curiosity how are you going to get around the captchas? Sence they have updated ive only seen it happen one time so maybe it wont be a big deal.
I was thinking bout looking for the swagbuck pic after ever search and if it finds it stop searching and ding the user.
*Edit Can you plz post the full code?
This is what I have its add the user names but no click
Code:
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Dim theElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("Input")
WebBrowser1.Document.GetElementById("emailAddressLocal").SetAttribute("value", My.Settings.User)
WebBrowser1.Document.GetElementById("pswdLocal").SetAttribute("value", My.Settings.Pass)
'WebBrowser1.Document.GetElementById("btn_login").InvokeMember("click")
'collect all <img> tag name
Dim elemCollec As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")
'iterate each <img> found to look for unique string of the button
For Each curElement As HtmlElement In theElementCollection
Dim controlName As String = curElement.GetAttribute(" tagname").ToString
If controlName = "frmLogIn-tbar" Then
WebBrowser1.Document.GetElementById("btn1 btnlogin").InvokeMember("click")
End If
Next
End Sub
Here is the code that I worked my arse off to get! look at the invokemember click statement. It took several weeks to get this one line right.
HTML Code:
Dim theWElementCollection As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("Input")
For Each curElement As HtmlElement In theWElementCollection
Dim controlName As String = curElement.GetAttribute("id").ToString
If controlName = "emailAddressLocal" Then
curElement.SetAttribute("Value", lblUser.Text)
ElseIf controlName = "pswdLocal" Then
curElement.SetAttribute("Value", lblPass.Text)
System.Threading.Thread.Sleep(3000)
WebBrowser1.Document.InvokeScript("logInTBLocal")
End If