[RESOLVED]Help with clicking login through webBrowser
Here is the source
HTML Code:
<td rowspan=2> </td><td><img src="http://www.locationary.com/web/img/LocationaryImgs/icons/txt_email.gif"></td>
<td><input class="Data_Entry_Field_Login" type="text" name="inUserName" id="inUserName" size="25"></td>
<td><img src="http://www.locationary.com/web/img/LocationaryImgs/icons/txt_password.gif"></td>
<td><input class="Data_Entry_Field_Login" type="password" name="inUserPass" id="inUserPass"></td>
<td style="padding-top:2"><input id="login@DEFAULT" type="image" src="http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_normal.gif" onmouseover="javascript:this.src='http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_highlighted.gif'" onmouseout="javascript:this.src='http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_normal.gif'" value="Submit" alt="Login" ></td></tr>
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Part 1: Use WebBrowser control to load web page
WebBrowser1.Navigate("http://www.locationary.com")
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
' Part 2: Username and password
Dim theElementCollection As HtmlElementCollection
theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each curElement As HtmlElement In theElementCollection
Dim controlName As String = curElement.GetAttribute("name").ToString
If controlName = "inUserName" Then
curElement.SetAttribute("Value", "myUsername")
ElseIf controlName = "inUserPass" Then
curElement.SetAttribute("Value", "myPassword")
End If
Next
WebBrowser1.Document.GetElementById("login@DEFAULT").InvokeMember("click")
End Sub
End Class
Everything works fine except the login, it gives me an "Object reference not set to an instance of an object" error.
Re: Help with clicking login through webBrowser
Re: Help with clicking login through webBrowser
Thanks for the reply, but im already using whats suggested in that link
Code:
WebBrowser1.Document.GetElementById("login@DEFAULT").InvokeMember("click")
Can someone tell me what the tagName for this button is
HTML Code:
<td style="padding-top:2"><input id="login@DEFAULT" type="image" src="http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_normal.gif" onmouseover="javascript:this.src='http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_highlighted.gif'" onmouseout="javascript:this.src='http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_normal.gif'" value="Submit" alt="Login" ></td></tr>
Re: Help with clicking login through webBrowser
using your html snippet +
vb Code:
WebBrowser1.Document.GetElementById("login@DEFAULT").InvokeMember("click")
it clicked the button as expected. can you post the full html?
Quote:
Originally Posted by
Datadayne
Can someone tell me what the tagName for this button is
HTML Code:
<td style="padding-top:2"><input id="login@DEFAULT" type="image" src="http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_normal.gif" onmouseover="javascript:this.src='http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_highlighted.gif'" onmouseout="javascript:this.src='http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_normal.gif'" value="Submit" alt="Login" ></td></tr>
the tagName is "input"
Re: Help with clicking login through webBrowser
Thanks for the reply, full html is too long to post, but here is the part of the html that includes the login:
HTML Code:
<form name="TopLoginForm" method="post" action="https://www.locationary.com/web/login/index.jsp?ACTION_TOKEN=tile_loginBar_jsp$JspView$LoginAction" id="m_Login">
<div style="float:right;">
<table border=0>
<tr>
<td rowspan=2> </td><td><img src="http://www.locationary.com/web/img/LocationaryImgs/icons/txt_email.gif"></td>
<td><input class="Data_Entry_Field_Login" type="text" name="inUserName" id="inUserName" size="25"></td>
<td><img src="http://www.locationary.com/web/img/LocationaryImgs/icons/txt_password.gif"></td>
<td><input class="Data_Entry_Field_Login" type="password" name="inUserPass" id="inUserPass"></td>
<td style="padding-top:2"><input id="login@DEFAULT" type="image" src="http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_normal.gif" onmouseover="javascript:this.src='http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_highlighted.gif'" onmouseout="javascript:this.src='http://www.locationary.com/web/img/LocationaryImgs/btn/white/btn_login_40x20_fff_normal.gif'" value="Submit" alt="Login" ></td></tr>
<tr><td></td><td ></td><td></td><td><a class="Medium " href="http://www.locationary.com/web/login/forgotpwd.jsp">Forgot Password?</a>
</td><td></td></tr>
</table>
</div>
</form>
Re: Help with clicking login through webBrowser
it worked with that too...
put this in a button + click it:
vb Code:
for each element as htmlelement in webbrowser1.document.getelementsbytagname("input")
msgbox(element.getattribute("id"))
next
does the submit button show up?
Re: Help with clicking login through webBrowser
Looks like it, Im getting inUserName, inPassName and login@DEFAULT.
Edit:
I tried adding this in the button click rather than in the webBrowser and its working for some odd reason.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Part 1: Use WebBrowser control to load web page
WebBrowser1.Navigate("http://www.locationary.com")
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
' Part 2: Username and password
Dim theElementCollection As HtmlElementCollection
theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each curElement As HtmlElement In theElementCollection
Dim controlName As String = curElement.GetAttribute("name").ToString
If controlName = "inUserName" Then
curElement.SetAttribute("Value", "myUser")
ElseIf controlName = "inUserPass" Then
curElement.SetAttribute("Value", "myPass")
End If
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
WebBrowser1.Document.GetElementById("login@DEFAULT").InvokeMember("click")
End Sub
End Class
Any reasons why its working if I plug it in the button rather than the webBrowser.
Re: Help with clicking login through webBrowser
the WebBrowser1_DocumentCompleted event fires more than once when navigating to a page. for an accurate measure of when the page is loaded check webbrowser1.readystate in your WebBrowser1_DocumentCompleted event
Re: Help with clicking login through webBrowser
You mean like this?
vb Code:
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
' Part 2: Username and password
Dim theElementCollection As HtmlElementCollection
theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each curElement As HtmlElement In theElementCollection
Dim controlName As String = curElement.GetAttribute("name").ToString
If controlName = "inUserName" Then
curElement.SetAttribute("Value", "user")
ElseIf controlName = "inUserPass" Then
curElement.SetAttribute("Value", "pass")
End If
Next
If WebBrowserReadyState.Complete Then
WebBrowser1.Document.GetElementById("login@DEFAULT").InvokeMember("click")
Else : MsgBox("WebBrowser did not complete")
End If
End Sub
The msgbox doesnt display so it should be completed, I switched wbreadystate.complete with .uninitialized and as expected the msgbox did display.
Re: Help with clicking login through webBrowser
that's how i meant, although you should put the other code inside the 'If WebBrowserReadyState.Complete Then' block too.
i'm guessing it didn't work, or you wouldn't be asking?
Re: Help with clicking login through webBrowser
Well switching the id of the login with the value seems to have worked.
vb Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Part 1: Use WebBrowser control to load web page
WebBrowser1.Navigate("http://www.locationary.com")
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
' Part 2: Username and password
Dim theElementCollection As HtmlElementCollection
theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each curElement As HtmlElement In theElementCollection
Dim controlName As String = curElement.GetAttribute("name").ToString
If controlName = "inUserName" Then
curElement.SetAttribute("Value", "user")
ElseIf controlName = "inUserPass" Then
curElement.SetAttribute("Value", "pass")
End If
Next
theElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
For Each curElement As HtmlElement In theElementCollection
Dim controlValue As String = curElement.GetAttribute("Value").ToString
If controlValue = "Submit" Then
curElement.InvokeMember("click")
End If
Next
End Sub
Although I still dont know why using the id doesnt work. Thanks for your time and help .paul.