How to use web browser control to automate login to a site
-
I was trying to login to a site programmatically. i am loading site into web browser control. i tried this code but it did not worked. https://auth.accounts.dowjones.com/login Sorry could not share actual credentials.
HtmlElement username = null;
HtmlElement password = null;
HtmlElement submit = null;foreach (HtmlElement divcontainer in webBrowser1.Document.GetElementsByTagName("div"))
{
if (divcontainer.GetAttribute("className") == "text-input")
{
foreach (HtmlElement child in divcontainer.Children)
{
if (divcontainer.GetAttribute("className") == "js-email-input")
{
username = divcontainer;
}if (divcontainer.GetAttribute("className") == "password js-password-input") { password = divcontainer; } } } if (divcontainer.GetAttribute("className") == "sign-in") { foreach (HtmlElement child in divcontainer.Children) { if (divcontainer.GetAttribute("className") == "solid-button basic-login-submit") { submit = divcontainer; } } }
}
if (username != null && password != null && submit != null)
{
username.SetAttribute("value", "test@gmail.com");
password.SetAttribute("value", "test11");
submit.InvokeMember("click");
}please some one guide me what to change in my code to automate login. Thanks
-
I was trying to login to a site programmatically. i am loading site into web browser control. i tried this code but it did not worked. https://auth.accounts.dowjones.com/login Sorry could not share actual credentials.
HtmlElement username = null;
HtmlElement password = null;
HtmlElement submit = null;foreach (HtmlElement divcontainer in webBrowser1.Document.GetElementsByTagName("div"))
{
if (divcontainer.GetAttribute("className") == "text-input")
{
foreach (HtmlElement child in divcontainer.Children)
{
if (divcontainer.GetAttribute("className") == "js-email-input")
{
username = divcontainer;
}if (divcontainer.GetAttribute("className") == "password js-password-input") { password = divcontainer; } } } if (divcontainer.GetAttribute("className") == "sign-in") { foreach (HtmlElement child in divcontainer.Children) { if (divcontainer.GetAttribute("className") == "solid-button basic-login-submit") { submit = divcontainer; } } }
}
if (username != null && password != null && submit != null)
{
username.SetAttribute("value", "test@gmail.com");
password.SetAttribute("value", "test11");
submit.InvokeMember("click");
}please some one guide me what to change in my code to automate login. Thanks
This seems logical. What's not working? "Did not work" does not help. Did it click the button? Did the text appear in the controls? Did it start playing Pacman?
-
This seems logical. What's not working? "Did not work" does not help. Did it click the button? Did the text appear in the controls? Did it start playing Pacman?
-
the code i have mention in my post which is not filling textbox and not clicking button. so help me where i made the mistake?
Either you didn't wait for the page to finish loading, or the elements in the page don't match the elements you're looking for.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Either you didn't wait for the page to finish loading, or the elements in the page don't match the elements you're looking for.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Issue sorted. i have done the job this way and it worked.
private void webBrowser1\_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && IsRedirect) { System.Threading.Thread.Sleep(5000); timer1.Enabled = true; } else if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && IsloginPage) { mshtml.IHTMLDocument2 htmlDocument = (mshtml.IHTMLDocument2) webBrowser1.Document.DomDocument; mshtml.IHTMLElementCollection objforms = htmlDocument.forms; foreach (mshtml.HTMLFormElementClass form in objforms) { if (form.className == "form") { IHTMLElementCollection inputElements = form.getElementsByTagName("INPUT"); foreach (HTMLInputElementClass inputElement in inputElements) { if ((inputElement.type.Trim().ToLower() == "email") && (inputElement.name.Trim() == "email")) { inputElement.value = "Test\_UID@gmail.com"; } else if ((inputElement.type.Trim().ToLower() == "password") && (inputElement.name.Trim() == "password")) { inputElement.value = "Test\_PWD"; } } IHTMLElementCollection inputElements1 = form.getElementsByTagName("BUTTON"); foreach (HTMLButtonElementClass inputElement in inputElements1) { if ((inputElement.innerText.ToLower() == "sign in") && (inputElement.type.Trim().ToLower() == "submit") && (inputElement.tagName.Trim().ToLower() == "button")) { inputElement.click(); break; } } } } } }
Thanks