So I have this application which will help me with my emailing for some of my customers and everything is working just fine until it gets to the last invoke. It presses the button and logs me in but then it keeps running the code and loops back to the button click again which throws me an error since the button isnt there anymore due to the fact that we already logged in so the HtmlDocument isnt the same anymore. I've tried to put a return; at the end but that didnt stop my code from running and im fairly lost at this point. I've tried debugging it with breakpoints and it seems to be, as I mentioned loop through the code again.
Exception Message: System.NullReferenceException: 'Object reference not set to an instance of an object.'
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NoTheme
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("https://accounts.google.com/ServiceLogin#identifier");
}
private void button1_Click(object sender, EventArgs e)
{
HtmlDocument startDoc = webBrowser1.Document;
HtmlElement usernameElement = startDoc.GetElementById("Email");
HtmlElement loginBtnElement = startDoc.GetElementById("signIn");
usernameElement.SetAttribute("value", "theGmail@gmail.com");
loginBtnElement.InvokeMember("click");
webBrowser1.DocumentCompleted += WebBrowser1_DocumentCompleted;
}
private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlDocument startDoc = webBrowser1.Document;
HtmlElement usernameElement = startDoc.GetElementById("Passwd");
HtmlElement loginBtnElement = startDoc.GetElementById("signIn");
usernameElement.SetAttribute("value", "thePassword"); //this is where it throws the error
loginBtnElement.InvokeMember("click");
}
}
}