In addition to 'solution 1':
You're searching for the input elements wrong. First of all you don't need to iterate over elements, you can search by ids:
private void DownloadLabelingListFile()
{
try
{
WebBrowser wb = new WebBrowser();
webBrowserForPrinting.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(ProcessDocument);
wb.Navigate(@"https://gateway.usps.com/bcg/login.htm");
}
catch (ThreadAbortException)
{
}
catch (HttpListenerException exc)
{
Logger.Err(Logger.LogFileType.ServiceReference, "DownloadLabelingListFile::CDownloader", exc);
}
}
private void ProcessDocument(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = (WebBrowser)sender;
HtmlElement userInputElement = wb.Document.GetElementById("login_name");
userInputElement.SetAttribute("Value", "***");
HtmlElement passInputElement = wb.Document.GetElementById("user_password");
passInputElement.SetAttribute("Value", "***");
}
I don't recommend using WebBrowser control for this kind of stuff, unless you really need the ability to execute javaScript inside page.
It would be much easier to do this with
System.Net.WebClient
or
System.Net.HttpWebRequest
classes.