How can I open a webpage in IE and fill in 1 or more text boxes in the page?
-
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); }
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[^]
-
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[^]
For whatever reason that code sample does not work.
-
For whatever reason that code sample does not work.
-
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");
} -
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");
}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?
-
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?
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[^]
-
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[^]
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?
-
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?
-
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
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
-
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
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?