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 can I open a webpage in IE and fill in 1 or more text boxes in the page?

How can I open a webpage in IE and fill in 1 or more text boxes in the page?

Scheduled Pinned Locked Moved C#
javascriptquestionphphtmldesign
17 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.
  • T Offline
    T Offline
    turbosupramk3
    wrote on last edited by
    #1

    This is my first time trying to do anything more than open a webpage as a process. I'd like to open a page in IE and then fill in a text box with the id being shown below in the small code snippet. I think I can use WebRequest for this, but I've tried a few code samples I found online and it does not open the webpage in IE, so I'm not sure where to start to get a simple example working and build off of it.

    <div class="formControl"><input name= "username" id= "txtlogin" maxlength= "75" placeholder= "Username" size= "25" type= "text" class= "" />
    </div>

    <html style="height: 100%;">

    <HEAD>
    <meta http-equiv="x-ua-compatible" content="IE=Edge"/>
    <meta http-equiv="X-UA-Compatible" content="chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Employee Self Service Login</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="robots" content="noindex">

    <script src="js/jquery-1.10.2.min.js"></script>
    <script src="js/jquery-ui-1.10.3.custom.min.js"></script>
    

    </HEAD>
    <BODY style="height: 100%;">

        Employee Self-Service
    
    
    
        <form method= "POST" action= "ee-loginproc.php" name= "frmEELogin" class= "" >
    
                                                <label class="standard" for="username">
    
                                                    Username
                                                </label>
    
                                                <input name= "username" id= "txtlogin" maxlength= "75" placeholder= "Username" size= "25" type= "text" class= ""  />
    
    
    
    
                                                <label class="standard" for="userpass">
    
                                                    Password
                                                </label>
    
                                                <input type= "password" name= "userpass" id
    
    T R 2 Replies Last reply
    0
    • T turbosupramk3

      This is my first time trying to do anything more than open a webpage as a process. I'd like to open a page in IE and then fill in a text box with the id being shown below in the small code snippet. I think I can use WebRequest for this, but I've tried a few code samples I found online and it does not open the webpage in IE, so I'm not sure where to start to get a simple example working and build off of it.

      <div class="formControl"><input name= "username" id= "txtlogin" maxlength= "75" placeholder= "Username" size= "25" type= "text" class= "" />
      </div>

      <html style="height: 100%;">

      <HEAD>
      <meta http-equiv="x-ua-compatible" content="IE=Edge"/>
      <meta http-equiv="X-UA-Compatible" content="chrome=1" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Employee Self Service Login</title>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      <meta name="robots" content="noindex">

      <script src="js/jquery-1.10.2.min.js"></script>
      <script src="js/jquery-ui-1.10.3.custom.min.js"></script>
      

      </HEAD>
      <BODY style="height: 100%;">

          Employee Self-Service
      
      
      
          <form method= "POST" action= "ee-loginproc.php" name= "frmEELogin" class= "" >
      
                                                  <label class="standard" for="username">
      
                                                      Username
                                                  </label>
      
                                                  <input name= "username" id= "txtlogin" maxlength= "75" placeholder= "Username" size= "25" type= "text" class= ""  />
      
      
      
      
                                                  <label class="standard" for="userpass">
      
                                                      Password
                                                  </label>
      
                                                  <input type= "password" name= "userpass" id
      
      T Offline
      T Offline
      turbosupramk3
      wrote on last edited by
      #2

      I found an addin called Selenium that will do this, but then I have to have additional dlls to my executable and it's 5mb, which is 10 times my exe's size and way overkill. No one has ever opened a page and then posted data to the input id's?

      L 1 Reply Last reply
      0
      • T turbosupramk3

        I found an addin called Selenium that will do this, but then I have to have additional dlls to my executable and it's 5mb, which is 10 times my exe's size and way overkill. No one has ever opened a page and then posted data to the input id's?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        turbosupramk3 wrote:

        No one has ever opened a page and then posted data to the input id's?

        Google will show quite some hits. If you want to manipulate IE, you will have to launch IE and manipulate that. If you want to fill in values in a webpage, you use a WebBrowser control. If you want to submit the data without any browser, use the WebRequest.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        T 1 Reply Last reply
        0
        • L Lost User

          turbosupramk3 wrote:

          No one has ever opened a page and then posted data to the input id's?

          Google will show quite some hits. If you want to manipulate IE, you will have to launch IE and manipulate that. If you want to fill in values in a webpage, you use a WebBrowser control. If you want to submit the data without any browser, use the WebRequest.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          T Offline
          T Offline
          turbosupramk3
          wrote on last edited by
          #4

          Hi, thanks for the response. I've been trying with webbrowser for the past hour or so. Depending on my code that I'm using, it will read the page and set the value, but not open the page with the set value. Or it will open the page but not set the value. I'm missing something that allows the two to work in harmony. Can you tell what I'm doing wrong? I believe I need to open the page, download the source, update the elements and reopen it in my browser, is this correct? I started with this: http://stackoverflow.com/questions/8425593/c-sharp-httpwebrequest-post-login-to-facebook/15772387#15772387[^]

          string email = "firstlast@gmail.com";
          string password = "12345678";
          string url = @"https://www.url.net/v4/ee/ee-login.php";

                      WebBrowser webbrowser1 = new WebBrowser();
                      webbrowser1.Navigate(url);
                      Application.DoEvents();
                      webbrowser1.Document.GetElementById("txtlogin").SetAttribute("value", email);
          
          L 1 Reply Last reply
          0
          • T turbosupramk3

            Hi, thanks for the response. I've been trying with webbrowser for the past hour or so. Depending on my code that I'm using, it will read the page and set the value, but not open the page with the set value. Or it will open the page but not set the value. I'm missing something that allows the two to work in harmony. Can you tell what I'm doing wrong? I believe I need to open the page, download the source, update the elements and reopen it in my browser, is this correct? I started with this: http://stackoverflow.com/questions/8425593/c-sharp-httpwebrequest-post-login-to-facebook/15772387#15772387[^]

            string email = "firstlast@gmail.com";
            string password = "12345678";
            string url = @"https://www.url.net/v4/ee/ee-login.php";

                        WebBrowser webbrowser1 = new WebBrowser();
                        webbrowser1.Navigate(url);
                        Application.DoEvents();
                        webbrowser1.Document.GetElementById("txtlogin").SetAttribute("value", email);
            
            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            turbosupramk3 wrote:

            Can you tell what I'm doing wrong?

            Using "DoEvents"; it is almost always a hint that there is something wrong. The "Navigate" method is not blocking, and DoEvents will return before the page is completely loaded. You want to wait with setting values until the document completes loading[^].

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            T 1 Reply Last reply
            0
            • L Lost User

              turbosupramk3 wrote:

              Can you tell what I'm doing wrong?

              Using "DoEvents"; it is almost always a hint that there is something wrong. The "Navigate" method is not blocking, and DoEvents will return before the page is completely loaded. You want to wait with setting values until the document completes loading[^].

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              T Offline
              T Offline
              turbosupramk3
              wrote on last edited by
              #6

              I'm trying but I can't get it, I feel like this is going to have to be spoon fed to me or I'm not going to get it? I've been trying an event based method (link to code below) as well. Stepping through the code, I can either get it to accept my element data and not open the page, or not accept my element data and open the default page, never can I get it to do both. Is the order of operations to load the page first and then submit the data, or do I need to ingest the data, update the elements and then load the page from the modified version? http://www.dreamincode.net/forums/topic/152297-c%23-log-in-to-website-programmatically/page__view__findpost__p__1229505[^]

              T 1 Reply Last reply
              0
              • T turbosupramk3

                I'm trying but I can't get it, I feel like this is going to have to be spoon fed to me or I'm not going to get it? I've been trying an event based method (link to code below) as well. Stepping through the code, I can either get it to accept my element data and not open the page, or not accept my element data and open the default page, never can I get it to do both. Is the order of operations to load the page first and then submit the data, or do I need to ingest the data, update the elements and then load the page from the modified version? http://www.dreamincode.net/forums/topic/152297-c%23-log-in-to-website-programmatically/page__view__findpost__p__1229505[^]

                T Offline
                T Offline
                turbosupramk3
                wrote on last edited by
                #7

                Like this?

                    private void editPage(object sender, WebBrowserDocumentCompletedEventArgs e)
                    {
                        webBrowser1.Document.GetElementById("txtlogin").SetAttribute("value", "my login");
                        webBrowser1.Document.GetElementById("txtpass").SetAttribute("value", "my password");
                    }
                
                    private void btnTestCalculate\_Click(object sender, EventArgs e)
                    {
                            string email = "firstlast@gmail.com";
                            string password = "12345678";
                            string url = @"https://www.url.net/v4/ee/ee-login.php";
                
                            webBrowser1 = new WebBrowser();                
                            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(editPage);
                            webBrowser1.Navigate(url, true);
                    }
                
                L 1 Reply Last reply
                0
                • T turbosupramk3

                  Like this?

                      private void editPage(object sender, WebBrowserDocumentCompletedEventArgs e)
                      {
                          webBrowser1.Document.GetElementById("txtlogin").SetAttribute("value", "my login");
                          webBrowser1.Document.GetElementById("txtpass").SetAttribute("value", "my password");
                      }
                  
                      private void btnTestCalculate\_Click(object sender, EventArgs e)
                      {
                              string email = "firstlast@gmail.com";
                              string password = "12345678";
                              string url = @"https://www.url.net/v4/ee/ee-login.php";
                  
                              webBrowser1 = new WebBrowser();                
                              webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(editPage);
                              webBrowser1.Navigate(url, true);
                      }
                  
                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Looks correct; open a browser, load a document, wait until loaded, adapt it's contents. Any redirections should be handled by the Webbrowser control. Do take in account that if you submit the page (ie, press login in the UI after this code runs) then it will navigate to another page, and the "document completed" event (or editpage) will be called again.

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                  T 1 Reply Last reply
                  0
                  • L Lost User

                    Looks correct; open a browser, load a document, wait until loaded, adapt it's contents. Any redirections should be handled by the Webbrowser control. Do take in account that if you submit the page (ie, press login in the UI after this code runs) then it will navigate to another page, and the "document completed" event (or editpage) will be called again.

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                    T Offline
                    T Offline
                    turbosupramk3
                    wrote on last edited by
                    #9

                    For whatever reason that code sample does not work.

                    L 1 Reply Last reply
                    0
                    • T turbosupramk3

                      For whatever reason that code sample does not work.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Does it throw an exception or does it do something different than expected?

                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                      T 1 Reply Last reply
                      0
                      • L Lost User

                        Does it throw an exception or does it do something different than expected?

                        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                        T Offline
                        T Offline
                        turbosupramk3
                        wrote on last edited by
                        #11

                        All it does is open the webpage. I put a break point at the

                        webBrowser1.Document.GetElementById("txtlogin").SetAttribute("value", "my login");

                        and it never catches the event. I then created a second document completed event as seen below and it isn't catching that either?

                        webBrowser1 = new WebBrowser();
                        //webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(editPage);
                        webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
                        webBrowser1.Navigate(url, true);

                        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
                        {
                        MessageBox.Show("Here");
                        }

                        T 1 Reply Last reply
                        0
                        • T turbosupramk3

                          All it does is open the webpage. I put a break point at the

                          webBrowser1.Document.GetElementById("txtlogin").SetAttribute("value", "my login");

                          and it never catches the event. I then created a second document completed event as seen below and it isn't catching that either?

                          webBrowser1 = new WebBrowser();
                          //webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(editPage);
                          webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
                          webBrowser1.Navigate(url, true);

                          void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
                          {
                          MessageBox.Show("Here");
                          }

                          T Offline
                          T Offline
                          turbosupramk3
                          wrote on last edited by
                          #12

                          Ok, I figured out why the event doesn't fire. If I put

                          webBrowser1.Navigate(url);

                          it will fire, but if I put

                          webBrowser1.Navigate(url, true);

                          it will not fire. The problem with that is if I use just

                          webBrowser1.Navigate(url);

                          then the browser does not open and nothing occurs visibly? Any ideas?

                          L 1 Reply Last reply
                          0
                          • T turbosupramk3

                            Ok, I figured out why the event doesn't fire. If I put

                            webBrowser1.Navigate(url);

                            it will fire, but if I put

                            webBrowser1.Navigate(url, true);

                            it will not fire. The problem with that is if I use just

                            webBrowser1.Navigate(url);

                            then the browser does not open and nothing occurs visibly? Any ideas?

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #13

                            Yes; add the webbrowser-control to your form and it will simply work. If the url is navigated to in an external browser, then it is no longer part of your application and will not receive any events. Below code should work and show the basic idea;

                            class Program
                            {
                                static WebBrowser \_wb;
                            
                                \[STAThread()\]
                                static void Main(string\[\] args)
                                {
                                    using (Form f = new Form())
                                    {
                                        \_wb = new WebBrowser() { Dock = DockStyle.Fill };
                                        f.Controls.Add(\_wb);
                                        \_wb.Navigate(@"http://www.gmail.com");
                                        \_wb.DocumentCompleted += wb\_DocumentCompleted;
                                        f.ShowDialog();
                                    }
                                }
                            
                                static void wb\_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
                                {
                                    \_wb.Document.GetElementById("Email").SetAttribute("value", "s.jobs@msdn.com");
                                }
                            }
                            

                            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                            T 1 Reply Last reply
                            0
                            • L Lost User

                              Yes; add the webbrowser-control to your form and it will simply work. If the url is navigated to in an external browser, then it is no longer part of your application and will not receive any events. Below code should work and show the basic idea;

                              class Program
                              {
                                  static WebBrowser \_wb;
                              
                                  \[STAThread()\]
                                  static void Main(string\[\] args)
                                  {
                                      using (Form f = new Form())
                                      {
                                          \_wb = new WebBrowser() { Dock = DockStyle.Fill };
                                          f.Controls.Add(\_wb);
                                          \_wb.Navigate(@"http://www.gmail.com");
                                          \_wb.DocumentCompleted += wb\_DocumentCompleted;
                                          f.ShowDialog();
                                      }
                                  }
                              
                                  static void wb\_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
                                  {
                                      \_wb.Document.GetElementById("Email").SetAttribute("value", "s.jobs@msdn.com");
                                  }
                              }
                              

                              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                              T Offline
                              T Offline
                              turbosupramk3
                              wrote on last edited by
                              #14

                              Thank you, this did work as far as logging me in. This is similar to something I had working yesterday with one of my attempts, it used a mshtml reference I believe. What steered me away from this yesterday is that it is the webbrowser control and not IE. I have to create a browser helper and not a stand alone browser. Is there any way to load the page into memory, login and then spawn the logged in page in IE and at that point disconnecting it from any application control? Am I trying to do something that cannot be done?

                              L 1 Reply Last reply
                              0
                              • T turbosupramk3

                                Thank you, this did work as far as logging me in. This is similar to something I had working yesterday with one of my attempts, it used a mshtml reference I believe. What steered me away from this yesterday is that it is the webbrowser control and not IE. I have to create a browser helper and not a stand alone browser. Is there any way to load the page into memory, login and then spawn the logged in page in IE and at that point disconnecting it from any application control? Am I trying to do something that cannot be done?

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #15

                                turbosupramk3 wrote:

                                I have to create a browser helper and not a stand alone browser.

                                :-D Well, that is a different story then. Google for "Browser Helper Object", and try some of those examples.

                                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                                1 Reply Last reply
                                0
                                • T turbosupramk3

                                  This is my first time trying to do anything more than open a webpage as a process. I'd like to open a page in IE and then fill in a text box with the id being shown below in the small code snippet. I think I can use WebRequest for this, but I've tried a few code samples I found online and it does not open the webpage in IE, so I'm not sure where to start to get a simple example working and build off of it.

                                  <div class="formControl"><input name= "username" id= "txtlogin" maxlength= "75" placeholder= "Username" size= "25" type= "text" class= "" />
                                  </div>

                                  <html style="height: 100%;">

                                  <HEAD>
                                  <meta http-equiv="x-ua-compatible" content="IE=Edge"/>
                                  <meta http-equiv="X-UA-Compatible" content="chrome=1" />
                                  <meta name="viewport" content="width=device-width, initial-scale=1.0">
                                  <title>Employee Self Service Login</title>
                                  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
                                  <meta name="robots" content="noindex">

                                  <script src="js/jquery-1.10.2.min.js"></script>
                                  <script src="js/jquery-ui-1.10.3.custom.min.js"></script>
                                  

                                  </HEAD>
                                  <BODY style="height: 100%;">

                                      Employee Self-Service
                                  
                                  
                                  
                                      <form method= "POST" action= "ee-loginproc.php" name= "frmEELogin" class= "" >
                                  
                                                                              <label class="standard" for="username">
                                  
                                                                                  Username
                                                                              </label>
                                  
                                                                              <input name= "username" id= "txtlogin" maxlength= "75" placeholder= "Username" size= "25" type= "text" class= ""  />
                                  
                                  
                                  
                                  
                                                                              <label class="standard" for="userpass">
                                  
                                                                                  Password
                                                                              </label>
                                  
                                                                              <input type= "password" name= "userpass" id
                                  
                                  R Offline
                                  R Offline
                                  Rahul Anand Jha
                                  wrote on last edited by
                                  #16

                                  The easiest way would be to use Selenium WebDriver.If you are using Visual Studio you could add it via Nuget Package Manager. Though its generally used for other purposes you can surely (ab)use it

                                  T 1 Reply Last reply
                                  0
                                  • R Rahul Anand Jha

                                    The easiest way would be to use Selenium WebDriver.If you are using Visual Studio you could add it via Nuget Package Manager. Though its generally used for other purposes you can surely (ab)use it

                                    T Offline
                                    T Offline
                                    turbosupramk3
                                    wrote on last edited by
                                    #17

                                    Thanks. I tried the Selenium web driver and didn't want to have an extra 5mb (or two) dll packaged in with my executable if I didn't have to. It did work however I was hoping to use the small portion of code they use to do that, within my own project and within the .net framework instead. If the driver can do it, I can do it natively. The question is how?

                                    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