What I am going to do here is to navigate to Gmail from within the WebForm. Along with that I will also pass the userId and password from the code and will then fire the onCLick event. Here’s how we can do it.First of all add the WebBrowserControl to your Windows Form. By default, it will get docked to its parent container.
Then, in the shown event of the form, add the following code:
private void BrowserNavigateTest_Shown(object sender, EventArgs e) { webBrowser1.Navigate("https://mail.google.com"); }
Finally, we’ll add the main code to the WebBrowser’s DocumentCompleted event. Here, we’ll be using a little javascript style coding where we will first pass the userId, password to the respective textboxes then, we’ll also fire the button click event
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (isFirstAttempt) { try { //get page element with id webBrowser1.Document.GetElementById("Email").InnerText = "emailId"; webBrowser1.Document.GetElementById("Passwd").InnerText = "pass"; //login in to account(fire a login button programetically) webBrowser1.Document.GetElementById("signIn").InvokeMember("click"); } catch { } finally { isFirstAttempt = false; } } }
Here, isFirstAttempt is a bool which detrmines if the shown event is fired for the first time or not.
Next, run the code and the following screen will appear automatically with the user not having to do anything.