[RESOLVED] Log in a webpage via vb application
Hi!
Here is my code that should fill in "user" and "password" fields on a webpage ("http://forgarden.ee/tellimus") and then click the button "submit" in webbrowser. But it doesn't work!
It gives me error:Object reference not set to an instance of an object.
Source code from webpage user and pass fields and button:
HTML Code:
...
<li>
<label for="email" class="required"><em>*</em>E-posti aadress</label>
<div class="input-box">
<input name="login[username]" value="" id="email" class="input-text required-entry validate-email" title="E-posti aadress" type="text">
</div>
</li>
<li>
<label for="pass" class="required"><em>*</em>Salasõna</label>
<div class="input-box">
<input name="login[password]" class="input-text required-entry validate-password" id="pass" title="Salasõna" type="password">
</div>
</li>
</ul>
<p class="required">* Nõutud väljad</p>
</div>
</div>
</div>
<div class="col2-set">
<div class="col-1 new-users">
<div class="buttons-set">
</div>
</div>
<div class="col-2 registered-users">
<div class="buttons-set">
<a href="http://forgarden.ee/tellimus/index.php/customer/account/forgotpassword/" class="f-left">Salasõna läks meelest?</a>
<button type="submit" class="button" title="Logi sisse" name="send" id="send2"><span><span>Logi sisse</span></span></button>
</div>
</div>
...
And here is my VB code:
Code:
Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser.DocumentCompleted
'Dim ForgardenURL As String
'Dim Pass As String
'Dim User As String
'User = My.Settings.ForgardenUser
'Pass = My.Settings.ForgardenPass
'ForgardenURL = My.Settings.ForgardenURL
'WebBrowser.Document.GetElementById("login[username]").SetAttribute("value", User)
'WebBrowser.Document.GetElementById("login[password]").SetAttribute("value", Pass)
'WebBrowser.Document.GetElementById("send").InvokeMember("click")
End Sub
What has gone wrong here?
Re: Log in a webpage via vb application
Try this
Code:
Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser.DocumentCompleted
'Dim ForgardenURL As String
'Dim Pass As String
'Dim User As String
'User = My.Settings.ForgardenUser
'Pass = My.Settings.ForgardenPass
'ForgardenURL = My.Settings.ForgardenURL
'WebBrowser.Document.GetElementById("email").SetAttribute("value", Your login Email)
'WebBrowser.Document.GetElementById("pass").SetAttribute("value", Your Password)
'WebBrowser.Document.GetElementById("send").InvokeMember("click")
End Sub
Re: Log in a webpage via vb application
It still gives the error like in the first post.
How could I write a code that fills the fields and clicks login button with HttpWebRequest POST Method? Never used it, maybe it'll work?
Re: Log in a webpage via vb application
Flaming hell...that was a braincracker...
I tried all the tricks and tips I could get my hands on...
Ended up with only one solution(that works):
Code:
Private Sub WebBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser.DocumentCompleted
Dim Username = WebBrowser.Document.All("login[username]") 'Searches for username field
Dim Password = WebBrowser.Document.All("login[password]") 'Searches for password field
Dim pass As String
Dim user As String
user = My.Settings.ForgardenUser 'doesn't work, think it doesn't convert "@" to string or smth!!!!
pass = My.Settings.ForgardenPass
If Username <> Nothing And Password <> Nothing Then
Username.InnerText = "[email protected]" 'set the user manually
Password.InnerText = pass
SendKeys.Send("{ENTER}")
'hits ENTER to log in instead of click via code(no code worked)
End If
End Sub
Could somebody explain to me:
* why I couldn't get invokemember("click") to work
* why an e-mail address stored in my. settings doesn't convert to string and populate username field?
Is there a better way to write such code?
Re: Log in a webpage via vb application
Quote:
* why I couldn't get invokemember("click") to work
* why an e-mail address stored in my. settings doesn't convert to string and populate username field?
It doesn't always. Sometimes the relevant member is not called "click". Sometimes the button just doesn't accept any Invoke.
You went looking for tags by ID and used its name, for a start. Second, the attribute is not necessarily "value" and, by the same token, "value" is not necessarily an attribute.
All webpage manipulation requires a degree of trial and error and results are not guaranteed even then. You have to remember that the webpage coder has done nothing to help you. The intention is that you use a webbrowser and mouse to navigate the site and everything is slanted that way. Any degree of 'automation' you are able to achieve, where there's no API expressly created for the purpose, is all bonus.
Re: Log in a webpage via vb application
Quote:
Originally Posted by
lkallas
Code:
user = My.Settings.ForgardenUser 'doesn't work, think it doesn't convert "@" to string or smth!!!!
Could somebody explain to me:
* why an e-mail address stored in my. settings doesn't convert to string and populate username field?
Answer to my own question is that when I changed the setting name from ForgardenUser to User then it started to work. Go figure!