Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to use web browser control to automate login to a site

How to use web browser control to automate login to a site

Scheduled Pinned Locked Moved C#
tutorialjavascriptcomarchitecture
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mou_kol
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • M Mou_kol

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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?

      M 1 Reply Last reply
      0
      • C Christian Graus

        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?

        M Offline
        M Offline
        Mou_kol
        wrote on last edited by
        #3

        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?

        Richard DeemingR 1 Reply Last reply
        0
        • M Mou_kol

          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?

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          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

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          M 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            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

            M Offline
            M Offline
            Mou_kol
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups