vb.net if statement on webbrowser control - vb.net

Morning! (atleast in the US its morning).
I have an app with multiple buttons that load different sites using the webbrowser control. I have the below code to auto login to an email website. However, if I browse to another site and then click the email signin button to execute the below code, it errors out. It errors out because it already signed me in to the site the first time and now since the credentials are cached it logs me right in and bypasses the login page.
How do I check if the login page doesn't exist, then exit sub.
WebBrowser1.Navigate(My.Settings.emailURL)
WaitForPageLoad()
WebBrowser1.Document.GetElementById("user").InnerText = My.Settings.emailUN
WebBrowser1.Document.GetElementById("passwd").InnerText = My.Settings.emailPW
'WebBrowser1.Document.GetElementById("remember_me").SetAttribute("checked", "false")
WebBrowser1.Document.DomDocument.forms(0).submit()
I tried the below but it didn't work:
If WebBrowser1.Navigate(My.Settings.emailURL) <> My.Settings.emailURL
Im assuming it didn't work because its a navigate command

Figured it out. The web address is unique if you're signed in and displays a static web url, so I did a small if statement.
If WebBrowser1.Url.AbsoluteUri.StartsWith(My.Settings.webmailURL)
Exit Sub
Else
Sign in......
end if
Hans thanks for the help, you got me thinking.

Related

How to load webpage in VB and automatically log in?

I have an application that will open a webpage using the Web browser control and automatically login the users information, for example gmail. I plan on having 3 choices the user can enter: Gmail, Twitter and Facebook. All the do it type in their username and password and click one of the options above, and their account will automatically be signed in. The problem I am experiencing is that when my application loads, so does gmail, so if the user wants to login to facebook, it would be awkward to see Gmail automatically loaded in the form instead, right? So I took ` WebBrowser1.Navigate("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1") from the private form, and wanted to add it into my button called gmail. So the button would look like this:
Private Sub btnGmail_Click(sender As Object, e As EventArgs) Handles btnGmail.Click
WebBrowser1.Navigate("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1")
WebBrowser1.Document.GetElementById("Email").SetAttribute("value", txtEmail.Text)
WebBrowser1.Document.GetElementById("Passwd").SetAttribute("value", txtPasswd.Text)
WebBrowser1.Document.GetElementById("signIn").InvokeMember("click")
End Sub
However I get the error:
An unhandled exception of type 'System.NullReferenceException'
Which from some Googling showed that it meant there was something NULL in my form? But everything is filled in, so I don't understand why? In my code, it underlines the "txtEmail.Text" part on the 3rd line, but I'm filling it in?
So now I am left clueless as to have the user click the button "GMAIL" so it will load gmail and proceed to input the information they type in the email and passwd textbox.

If on URL then message box

So I have this weird problem. I have a WebBrowser starting on the page https://live.xbox.com/en-US/MyXbox/GamerProfile (It is a page for editing information on your account) but whenever it goes there it takes the user to live.com to log in, and then goes back to the page to edit his profile. This is how I want it to function, but I want a message box to display if it takes the user to the page again (to verify he's logged in). This is what I have so far:
If WebBrowser1.Url.OriginalString() = "https://live.xbox.com/en-US/MyXbox/GamerProfile" Then
MessageBox.Show("You are logged in!")
End If
But I never get the message box to show, and I don't think it works :\
Does anyone know why it isn't working?
which part of your application is that code running? I mean, does your program knows when xbox takes the webrowser to the login page and back? If your code only run once when the webbrowser object is created, then this doesn't get run.
EDIT:
In that case, the code should be within the WebBrwoser.DocumentCompleted() event. Take a look at this: http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentcompleted.aspx

VB.Net button open multiple user and auto login in different window of IE

I have a website stored 100 users ....now i want to create a VB form with 100 buttons ....while i click button1, it will open IE then log in users1 automatic and when i click button2 , it will open IE then log in as user2 automatic. user3 until user100 the same ....click from button on vb form.
Note : i am already done to set open IE and log in as different users in each windows but now i am finding how to set auto log in with different users in the same website when i click each button in form.
i have something more about form....i will use ( 100 button click = 100 users = 100 IE window ....)
it depend on user click on button that he need to log in......because we don't know that what button/users/time will he want to click .....
I really need your help urgently...
Thank in advanced.
here is some code of Button_Click
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Using p As New Process
p.StartInfo.FileName = "C:\Program Files\Internet Explorer\iexplore.exe"
p.StartInfo.Arguments = "http://test.com"
p.Start()
End Using
This is the only way I can come up with on how to solve this.
My guess is that your login is using POST variables, it's pretty much standard.
To make this work you would have to allow the username and password to be supplied with GET. That is, sending them in the url like this:
http://test.com/login.php/asp?username=username&password=password
Since you haven't specified whether you use PHP or ASP.NET I will add info on how to get the value for both:
ASP.NET
Request.Querystring("username")
PHP
$_GET["username"];
EDIT based on comment
You are correct it is not possible to isolate separate webbrowser controls.
I would suggest you look into either WaTin to control separate IE instances, or Awsomium .NET, which i believe allows separate sessions in 1.7, though i haven't tested it.
Also be aware that since IE8 session cookies are shared across instanced by default, so you would need to run them with the -nomerge flag. WaTin supports this.
Based on the fact that you have not mentioned this in your question, i am presuming you are using IE7. If that is the case, and this application is not intended to be used on other machines, it should be possible to create a separate application with an embeded webbrowser control, then launch multiple insatances of that application from the main one, so you can add your own communication mechanism, but WaTin is probably a far better idea
OLD ANSWER:
It is very hard to work out what you want to do, but im guessing you want to create a winforms app, that lets you to choose a user account, and then open a browser and login to a website with that account.
Based on that assumption, my suggestion would be to have one button, and some way of selecting the account, say a comboBox, and a webbrowser control:
http://img198.imageshack.us/img198/888/66649645.jpg
Rather than 1 button per user account and trying to manipulate an external browser.
If this is indeed what you want to do, comment and i will edit my answer as required, and provide starting code if you need, but i cant do that without really undertanding what you need to do.

Operating on a web page using the Document Object

I have a VB.Net app that needs to print a bunch of pages from a secure intranet site; the app was made and was working for a non-secure site but now the app needs to login before accessing the web pages necessary to print.
I get the error "Object Reference not set to an instance of an object" when I use my new Sub which follows:
Private Sub SiteLogin()
WebBrowser1.Navigate("http://url/login/")
WebBrowser1.Document.GetElementById("user").SetAttribute("value", "username")
WebBrowser1.Document.GetElementById("password").SetAttribute("value", "mypassword")
WebBrowser1.Document.GetElementById("submit").InvokeMember("click")
End Sub
I got the code from this VB tutorial, but I seem to be missing something in how I'm referencing the WebBrowser.Document object. I've never used VB.Net before so I don't know much about this problem, I have the following sub which DOES work from the original program however:
Private Sub WebPrint()
WebBrowser1.Navigate(strUrl)
WebDocLoaded = False
'lblMessage.Text = "Waiting for report page to load"
Do Until WebDocLoaded
System.Windows.Forms.Application.DoEvents()
Loop
End Sub
This Sub prints the given page in IE, so I figured I could use the same WebBrowser1 object for the login Sub, as I don't know how to or whether I should declare a separate object for the SiteLogin Sub. Both of these functions are called after the WebBrowser1 object is defined however.
I just need to be able to submit a simple login form to a set URL, and this info needs to all be hardcoded (there's no option to set the location or login info nor should there be). If there's another way to log into this site via VB.Net or if I need to give more information let me know.
.Navigate returns before the page load is complete so the DOM is not ready & .Document is not accessible.
See; How to wait until WebBrowser is completely loaded in VB.NET?

VB.NET - Log into website with out website open

I want to create a program to fetch information from a website (that's fine). However, you need to be logged in to get this information.
I just want two simple textbox controls that I would type my username and password into and hit submit and the information would be returned to me so I can use it. (I don't want to use a webbrowser control)
Sorry if I wasn't clear
And also
(this really isn't important, but its up to you if you want to answer, it's probably hard)
How would I go about checking if I am still logged into a website or not (as in TRUE or FALSE)
you can pass credentials into a HttpWebRequest object.
You could use a webbrowser control since it is easy, and just set its display to not visible - the user would never see anything. You could still interact with the page just like normal. You could use the same control to log yourself out by clicking on the logout button. You would set everything up and test it with visible=true, and when it all works, set visible to false.