|
-
Oct 30th, 2010, 04:42 PM
#1
Thread Starter
New Member
Click Forum Login Button
Hi!
I'm trying to make a program that will automatically log me into a forum. I have the webbrowser control and a button. the site is automatically loaded on form load. I want it to log me in using my username and password when i click the button. I tried it on yahoo.com to test and worked fine. But for some reason when I try it with my forum it doesn't work. My forum is SMF. It seems that the problem lies with the Login button itself. I can easily send the username and password to the corresponding boxes but when it comes to clicking the Login button it fails. What makes that Login button different from the Login button of Yahoo other than having a different name? Below is an example of the code I was using. How can I get it to successfully click the SMF Login button? Please post example code if possible.
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
WebBrowser1.Document.GetElementById("vb_login_username").SetAttribute("value", TextBox1.Text)
WebBrowser1.Document.GetElementById("vb_login_password").SetAttribute("value", TextBox2.Text)
WebBrowser1.Document.GetElementById("Login").InvokeMember("click")
End Sub
Best,
~Chris
-
Oct 30th, 2010, 04:52 PM
#2
Re: Click Forum Login Button
what's the HTML code for the button? also, have you made sure that the actual textboxes are getting populated fully before "clicking" the button? might need to put the "clicking" line in a timer and check to see if the textboxes contain a value before issuing the "click"
-
Oct 30th, 2010, 05:29 PM
#3
Thread Starter
New Member
Re: Click Forum Login Button
I know everything works fine because I tried having it fill the text boxes and not click the login button and it filled them correctly. Then I add the Login code after that. On a site like yahoo it works fine. Click the button and it sends the correct info to the text boxes of the yahoo page and clicks the login button and I'm logged in. Works perfect.
I use the GetElementById method to fill the boxes based on their Id in the html code. (re: code I posted above) I use the same thing for the Login button and InvokeMember("click") to make it click the button. All the Id names are correct. Why is it not working? I heard somewhere it has to do with how smf processes the login or something. Does anybody know how this works? Please explain.
-
Oct 30th, 2010, 05:33 PM
#4
Re: Click Forum Login Button
...or don't post the source code... stop comparing it to yahoo.. it isn't yahoo.
i had to go find another smf-based forum (see previous post's first request) and nowhere in it did I find an HTML element with the ID "Login" so... that's probably your problem.
-
Oct 30th, 2010, 05:39 PM
#5
Thread Starter
New Member
Re: Click Forum Login Button
Here is the Login code on the Login Page:
Code:
<div class="roundframe">
<dl>
<dt>Username:</dt>
<dd><input type="text" name="user" size="20" value="" class="input_text" /></dd>
<dt>Password:</dt>
<dd><input type="password" name="passwrd" value="" size="20" class="input_password" /></dd>
</dl>
<dl>
<dt>Minutes to stay logged in:</dt>
<dd><input type="text" name="cookielength" size="4" maxlength="4" value="60" class="input_text" /></dd>
<dt>Always stay logged in:</dt>
<dd><input type="checkbox" name="cookieneverexp" class="input_check" onclick="this.form.cookielength.disabled = this.checked;" /></dd>
</dl>
<p><input type="submit" value="Login" class="button_submit" /></p>
<p class="smalltext"><a href="http://mydomain.com/index.php?action=reminder">Forgot your password?</a></p>
<input type="hidden" name="hash_passwrd" value="" />
</div>
This is from an smf forum. It uses "Login" as the Id. See:
Code:
<p><input type="submit" value="Login" class="button_submit" /></p>
So wrong Id is not my problem. I used all the Ids mentioned in the above code.
-
Oct 30th, 2010, 06:51 PM
#6
Re: Click Forum Login Button
then you need an HTML lesson..
<p><input type="submit" value="Login" class="button_submit" /></p>
i don't see the word "id" in that line ANYWHERE
you're going to have to loop through the HTMLCollection of "input" elements and check the attribute "value" to see if it equals "Login", if it does, invoke the click method.
for kicks, here's the code for the yahoo login page:
HTML Code:
<div id='inputs'>
<label for='username'>Yahoo! ID</label>
<input name='login' id='username' maxlength='96' tabindex='1'>
<p id='ex'>(e.g. [email protected])</p>
<label for='passwd'>Password</label>
<input name='passwd' id='passwd' type='password' maxlength='64' tabindex='2'>
</div>
notice anything? the username and password input fields have ID's (username and 'passwd', respectively.
try this:
vb.net Code:
Dim webBrowserDocument As HtmlDocument = webBrowser.Document
Dim hec As HtmlElementCollection = webBrowserDocument.GetElementsByTagName("input")
For Each element As HtmlElement In hec
If element.GetAttribute("name") = "Login" Then
'Invoke here
End If
Next
Last edited by stateofidleness; Oct 30th, 2010 at 07:09 PM.
-
Oct 31st, 2010, 05:00 PM
#7
Thread Starter
New Member
Re: Click Forum Login Button
Ok this is starting to make sense. For the sake of simplifying things I created 2 buttons. Button1 sends the username and password to the corresponding boxes in the forum. Button2 clicks the Login button.
Button1 code:
Code:
Me.WebBrowser1.Document.GetElementById("user").SetAttribute("value", "myusername")
Me.WebBrowser1.Document.GetElementById("passwrd").SetAttribute("value", "mypassword")
Button2 code:
Code:
Dim webBrowserDocument As HtmlDocument = WebBrowser1.Document
Dim hec As HtmlElementCollection = webBrowserDocument.GetElementsByTagName("input")
For Each element As HtmlElement In hec
If element.GetAttribute("name") = "Login" Then
'InvokeMember Here???????????????
End If
Next
I'm new to VB. So, I'm a bit confused on how to use the InvokeMember in this case. It was easy in my first example with the yahoo page. But this is a little different. How would I type it out?
-
Oct 31st, 2010, 06:00 PM
#8
Re: Click Forum Login Button
element.InvokeMember("click") where it says "Invoke here"
-
Oct 31st, 2010, 08:09 PM
#9
Thread Starter
New Member
Re: Click Forum Login Button
It didn't work. I click the button and it does nothing. Code below:
Code:
Dim webBrowserDocument As HtmlDocument = WebBrowser1.Document
Dim hec As HtmlElementCollection = webBrowserDocument.GetElementsByTagName("input")
For Each element As HtmlElement In hec
If element.GetAttribute("name") = "Login" Then
element.InvokeMember("click")
End If
Next
-
Oct 31st, 2010, 09:00 PM
#10
Re: Click Forum Login Button
what's the url of the website you're working with?
-
Oct 31st, 2010, 09:55 PM
#11
Addicted Member
Re: Click Forum Login Button
OT
can you give source code to log in on yahoo..mesenger
-
Oct 31st, 2010, 10:06 PM
#12
Re: Click Forum Login Button
why not post the actual site you're trying to log into?
-
Nov 1st, 2010, 06:59 AM
#13
Thread Starter
New Member
Re: Click Forum Login Button
Website is udgclan.com. But this will be the same exact for all sites using smf 2.0RC3 for their forum.
-
Nov 1st, 2010, 08:43 AM
#14
Fanatic Member
Re: Click Forum Login Button
Why not just do
Code:
WebBrowser1.document.forms(0).invokemember("submit")
If I helped you out, please take the time to rate me 
-
Nov 1st, 2010, 12:32 PM
#15
Thread Starter
New Member
Re: Click Forum Login Button
It still isn't working. I apologize if I'm a bit retarded at times. I really am new at this and I'm trying to grasp what I'm doing. Did I insert the code into the right spot? Code below:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim webBrowserDocument As HtmlDocument = WebBrowser1.Document
Dim hec As HtmlElementCollection = webBrowserDocument.GetElementsByTagName("input")
For Each element As HtmlElement In hec
If element.GetAttribute("name") = "Login" Then
WebBrowser1.Document.Forms(0).InvokeMember("button_submit")
End If
Next
End Sub
-
Nov 1st, 2010, 12:53 PM
#16
Re: Click Forum Login Button
Well, you're combining the two suggestions, which really doesn't work in this case:
either do:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim webBrowserDocument As HtmlDocument = WebBrowser1.Document
Dim hec As HtmlElementCollection = webBrowserDocument.GetElementsByTagName("input")
For Each element As HtmlElement In hec
If element.GetAttribute("name") = "Login" Then
element.InvokeMember("click")
End If
Next
End Sub
or the other suggestion (which is a good suggestion):
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebBrowser1.Document.Forms(0).InvokeMember("submit") 'not button_submit
End Sub
-
Nov 1st, 2010, 01:01 PM
#17
Thread Starter
New Member
Re: Click Forum Login Button
It finally worked!!! Thank you so much!!!
The first option didn't work. It was the second option that worked for me. The below code successfully clicks the login button on an smf forum.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebBrowser1.Document.Forms(0).InvokeMember("submit") 'not button_submit
End Sub
-
Nov 1st, 2010, 01:14 PM
#18
Re: Click Forum Login Button
Good deal. Sometimes the Forms way (the way that worked) can be troublesome, like if you wanted to add a Poll or a contact form to the main page, it might re-order the form's place in the HTML document.
-
Nov 1st, 2010, 07:37 PM
#19
Fanatic Member
Re: Click Forum Login Button
Yes, but if that was the case you would take another route to do it.
Something like:
Code:
Dim htmEle As HtmlElement = (From elem In WebBrowser1.Document.GetElementsByTagName("input") Where elem.OuterHtml.ToLower.Contains("class=button_submit") Select elem)(0)
htmEle.InvokeMember("click")
If I helped you out, please take the time to rate me 
-
Nov 1st, 2010, 08:15 PM
#20
Fanatic Member
Re: Click Forum Login Button
look at the code here " If element.GetAttribute("name") = "Login" Then"
now look closer here "GetAttribute("name")" was there a "name" in the button call " Login"? no that should be "value"
 Originally Posted by DeepMagic22
It still isn't working. I apologize if I'm a bit retarded at times. I really am new at this and I'm trying to grasp what I'm doing. Did I insert the code into the right spot? Code below:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim webBrowserDocument As HtmlDocument = WebBrowser1.Document
Dim hec As HtmlElementCollection = webBrowserDocument.GetElementsByTagName("input")
For Each element As HtmlElement In hec
If element.GetAttribute("name") = "Login" Then
WebBrowser1.Document.Forms(0).InvokeMember("button_submit")
End If
Next
End Sub
Should look like this
Code:
Dim webBrowserDocument As HtmlDocument = WebBrowser1.Document
Dim hec As HtmlElementCollection = webBrowserDocument.GetElementsByTagName("input")
For Each element As HtmlElement In hec
If element.GetAttribute("value") = "Login" Then
element.InvokeMember("click")
End If
Next
Last edited by newprogram; Nov 1st, 2010 at 08:19 PM.
Live life to the fullest!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|