Using a WebBrowser
Hi. I started to use the WebBrowser control for the very first time in C#. I'm trying to do something pretty simple: to write a text in a textbox and then to click a button. I used to use the WebBrowser in VB, but the way it works in VB is different than it does in C#. All I succeeded to do was navigation, which works quite the same way as in VB:
myWebBrowser.Navigate("http://www.MyUrl.com");
I wrote the following HTML code:
<html>
<head>
<title>Simple form</title>
</head>
<body>
<form name="form1" method="post" action="a.php">
Password: <input type="password" name="textbox1">
<input type="submit" name="submit1">
</form>
</body>
</html>
Say I want to write into textbox1 the caption "12345" and then to click the submit1 button. How do I do that using C#?
Thanks in advance, Tal.
[970 byte] By [
Talikag] at [2007-11-20 10:57:51]

# 1 Re: Using a WebBrowser
the way i find it is almost the same as the VB version although there is a slight changes in the properties/methods in terms of names but still the whole picture conforms to the DOM.. it would be helpful if you simply add an "id" attribute to each of the HTML elements so that they can be easily referenced from the document..
webBrowser1.Navigate(@"<url>");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
webBrowser1.Document.GetElementById("textbox1").InnerText = "12345";
webBrowser1.Document.GetElementById("submit1").InvokeMember("click");
# 2 Re: Using a WebBrowser
Thank you very much. It works excellent :)
I rated your post.
Tal.
# 3 Re: Using a WebBrowser
Hi there. I'm having a little problem here.
I'm trying to write something in a textbox, then to click a button, and then, after redirecting (the button I click directs to another page), to do the first 2 actions again in a different page.
Unfortunatly, the actions I write after the redirection return an error, altought the page does change.
I wrote something like that
webBrowser1.Navigate(@"SomeUrl");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
webBrowser1.Document.GetElementById("username").InnerText = "someUsername";
webBrowser1.Document.GetElementById("password").InnerText = "somePassword";
webBrowser1.Document.GetElementById("submit1").InvokeMember("click");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
webBrowser1.Document.GetElementByTag("textarea")[0].InnerText = "someText";
webBrowser1.Document.GetElementByTag("input")[3].InvokeMember("click");
What am I doing wrong?
Thanks in advance, Tal.
# 4 Re: Using a WebBrowser
change "GetElementsByTag" to "GetElementsByTagName"...
webBrowser1.Document.GetElementByTagName("textarea")[0].InnerText = "someText";
webBrowser1.Document.GetElementByTagName("input")[3].InvokeMember("click");
# 5 Re: Using a WebBrowser
Hi there. I wrote the code above from the head - it's not exactly the code I really wrote. The error appears only after I compile, not before.
I tried to do something like that:
webBrowser1.Navigate(@"SomeUrl");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
webBrowser1.Document.GetElementById("username").InnerText = "someUsername";
webBrowser1.Document.GetElementById("password").InnerText = "somePassword";
webBrowser1.Document.GetElementById("submit1").InvokeMember("click");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
webBrowser1.Document.GetElementById("username").InnerText = "someUsername";
webBrowser1.Document.GetElementById("password").InnerText = "somePassword";
webBrowser1.Document.GetElementById("submit1").InvokeMember("click");
Instead of something like:
webBrowser1.Navigate(@"SomeUrl");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
webBrowser1.Document.GetElementById("username").InnerText = "someUsername";
webBrowser1.Document.GetElementById("password").InnerText = "somePassword";
webBrowser1.Document.GetElementById("submit1").InvokeMember("click");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
webBrowser1.Document.GetElementByTag("textarea")[0].InnerText = "someText";
webBrowser1.Document.GetElementByTag("input")[3].InvokeMember("click");
And it didn't return an error, altought it didn't change anything in the new page.
It's like it changes the page but still think about it as the first page. I just don't get it =/
Should I write something once the page redirects?
# 6 Re: Using a WebBrowser
the first two lines of the code ensures that first page document is completely loaded..
webBrowser1.Navigate(@"SomeUrl");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
the next three lines must refer to the first page document (element ids)..
webBrowser1.Document.GetElementById("username").InnerText = "someUsername";
webBrowser1.Document.GetElementById("password").InnerText = "somePassword";
webBrowser1.Document.GetElementById("submit1").InvokeMember("click");
and the next line ensures that the second page is completely loaded..by the way, the Application.DoEvents() must be called prior to the condition in the loop so you may change it to do-while..
do Application.DoEvents(); while (webBrowser1.ReadyState != WebBrowserReadyState.Complete);
and finally, the remaining lines should refer to the second page document (element ids)..
webBrowser1.Document.GetElementById("username").InnerText = "someUsername";
webBrowser1.Document.GetElementById("password").InnerText = "somePassword";
webBrowser1.Document.GetElementById("submit1").InvokeMember("click");
# 7 Re: Using a WebBrowser
Thanks much for the post, but unfortunatly it still won't work.
I really don't know what I'm doing wrong.
Here's the first code, that one doesn't work (although it has to):
webBrowser1.Navigate(@"http://pointserv.net:2222/CMD_FILE_MANAGER/domains/affectingmusic.co.il/public_html/1.txt?action=edit");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
webBrowser1.Document.GetElementById("username").InnerText = "SomePass";
webBrowser1.Document.GetElementById("password").InnerText = "SomePassword";
webBrowser1.Document.GetElementsByTagName("input")[3].InvokeMember("click");
do
Application.DoEvents();
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete);
webBrowser1.Document.GetElementById("text").InnerText = IP;
webBrowser1.Document.GetElementsByTagName("input")[4].InvokeMember("click");
Here's another code I wrote, this one worked properly, without returning any errors (it doesn't have to work, though):
webBrowser1.Navigate(@"http://pointserv.net:2222/CMD_FILE_MANAGER/domains/affectingmusic.co.il/public_html/1.txt?action=edit");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
webBrowser1.Document.GetElementById("username").InnerText = "SomePass";
webBrowser1.Document.GetElementById("password").InnerText = "SomePassword";
webBrowser1.Document.GetElementsByTagName("input")[3].InvokeMember("click");
do
Application.DoEvents();
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete);
webBrowser1.Document.GetElementById("username").InnerText = "SomePass";
webBrowser1.Document.GetElementById("password").InnerText = "SomePassword";
webBrowser1.Document.GetElementsByTagName("input")[3].InvokeMember("click");
The last 3 lines won't do anything, of course. But just the fact that they don't return an error annoying me, because there are no controls in the second page called "username" or "password".
As I said earlier, I think that the program thinks the first page as the page in the web browser even after it's redirected.
Note: The page DOES redirect. I can see it in the web-browser once I run this code (the 6 first lines in the codes above):
webBrowser1.Navigate(@"http://pointserv.net:2222/CMD_FILE_MANAGER/domains/affectingmusic.co.il/public_html/1.txt?action=edit");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
webBrowser1.Document.GetElementById("username").InnerText = "SomePass";
webBrowser1.Document.GetElementById("password").InnerText = "SomePassword";
webBrowser1.Document.GetElementsByTagName("input")[3].InvokeMember("click");
# 8 Re: Using a WebBrowser
Hi. I solved the problem by using the DocumentComplete event.
Thread1, thanks very much for your help. You helped me a lot!
Tal.