Reputation: 375
I'm writing writing a program to perform some basic tests and I'm trying to get it to automatically login. It seems that the documentComplete event would be overused if I had to use it for every possible scenario.
I saw a number of examples with Browser ReadyState but I'm unable to get them to work.
I have a simple method which finds the username element, enters the user name, invokes the click method, finds the password element, enters the password, and invokes the click method.
If I delay the process with a messagebox then it works because it gives the page time to load.
private void fillInLogin()
{
browser.Document.GetElementById("des_LoginName").InnerText = "myLogon";
browser.Document.GetElementById("btnLogOn").InvokeMember("click");
//*********WHAT CAN I PUT HERE TO CHECK TO SEE IF THOSE ELEMENTS EXIST***********
browser.Document.GetElementById("des_Password").InnerText = "myPassword";
browser.Document.GetElementById("btnSubmit").InvokeMember("click");
}
Upvotes: 0
Views: 475
Reputation: 6604
Have you looked into subscribing to the WebBrowser.DocumentCompleted
event? It takes all of the guesswork out of waiting for a page to load, and is actually the proper way to do it. You can't really rely on a server response to be completed within a given wait period. Usually the only guarantee is the timeout period of your client code, but you don't want to force a wait to the timeout maximum. Using a threaded request and an event listening for the callback will allow you to make the request and continue to provide a responsive UI to the user.
Upvotes: 1