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 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