Hello, I seem to have a problem that is annoying me very badly. Heres the thing, I want to be able to change Text, and be able to press buttons/links on a WebBrowser, while controlling it from the program. As for my first try, I decided to change the Text of two text boxes, then press a button. The thing I was doing this on was a "PM" consept. The way I did it was by changing the WebBrowsers Url to "javascript:such and such" I don't know if this is the most effective way to do things, but thats what I did. Heres, the code I used:
Me.Browser.Url = New System.Uri("javascript:document.getElementById('ctl00_ctl00_cphRoblox_cphMyRobloxContent_rbxMessageEditor_txtSubject').value='" + Me.SubjectBox.Text + "'", System.UriKind.Absolute)
Me.Browser.Url = New System.Uri("javascript:document.getElementById('ctl00_ctl00_cphRoblox_cphMyRobloxContent_rbxMessageEditor_txtBody').value='" + Me.BodyBox.Text + "'", System.UriKind.Absolute)
Me.Browser.Url = New System.Uri("javascript:document.getElementById('ctl00_ctl00_cphRoblox_cphMyRobloxContent_lbSend').click()", System.UriKind.Absolute)
Could someone help me in order to change textbox's and pres buttons/links on a webpage from the program itself? Thanks, Zack - Changed TypeAland LiMSFT, ModeratorTuesday, September 22, 2009 10:03 AMNo reply
-
|
| NintendoZACHERY Monday, September 14, 2009 11:59 PM |
Use the WebBrowser.Document property.
Hans Passant. |
| nobugz Tuesday, September 15, 2009 1:17 AM |
When I try it doesn't do exactly what I want it to, Mind showing just examples on how to add text to a text box, and how to click a button? That would help so much. |
| NintendoZACHERY Tuesday, September 15, 2009 1:18 AM |
This sample form runs a Google query: public partial class Form1 : Form { public Form1() { InitializeComponent(); webBrowser1.Url = new Uri("http://google.com"); webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted; } void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (webBrowser1.Url.Host.EndsWith("google.com")) { HtmlDocument doc = webBrowser1.Document; HtmlElement ask = doc.All["q"]; HtmlElement lucky = doc.All["btnI"]; ask.InnerText = "nobugz"; lucky.InvokeMember("click"); } } }
Hans Passant.- Marked As Answer byAland LiMSFT, ModeratorThursday, September 17, 2009 3:03 AM
- Unmarked As Answer byNintendoZACHERY Thursday, September 17, 2009 3:05 AM
- Unmarked As Answer byNintendoZACHERY Thursday, September 17, 2009 3:02 AM
- Marked As Answer byAland LiMSFT, ModeratorWednesday, September 16, 2009 6:51 AM
-
|
| nobugz Tuesday, September 15, 2009 1:59 AM |
that doesn't look like VB.NET Sorry if I'm in the wrong forum, I was recommended to ask here. |
| NintendoZACHERY Tuesday, September 15, 2009 5:48 AM |
Hi NintendoZACHERY, Nobugz's code is in C#, the VB.Net code is:
Public Class Form1
Public Sub New()
InitializeComponent()
WebBrowser1.Url = New Uri("http://google.com")
AddHandler WebBrowser1.DocumentCompleted, AddressOf webBrowser1_DocumentCompleted
End Sub
Private Sub webBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
If WebBrowser1.Url.Host.EndsWith("google.com") Then
Dim doc As HtmlDocument = WebBrowser1.Document
Dim ask As HtmlElement = doc.All("q")
Dim lucky As HtmlElement = doc.All("btnI")
ask.InnerText = "nobugz"
lucky.InvokeMember("click")
End If
End Sub
End Class
Regards, Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. - Marked As Answer byNintendoZACHERY Thursday, September 17, 2009 3:02 AM
- Unmarked As Answer byNintendoZACHERY Thursday, September 17, 2009 3:05 AM
-
|
| Aland Li Wednesday, September 16, 2009 6:50 AM |
I do understand it, but how would I find things in the document by id, and change them?
Thanks so far. I just need to know how to do that, and my program should become done soon. |
| NintendoZACHERY Thursday, September 17, 2009 1:50 AM |
Just look at the HTML of the web page. I use Firebug on Firefox.
Hans Passant. |
| nobugz Thursday, September 17, 2009 3:28 AM |
I found the names of the text box's I wanted, but I need to use Id for the button. And for some reason the text doesn't change. |
| NintendoZACHERY Thursday, September 17, 2009 3:29 AM |
Hi NintendoZACHERY,
Did you find the button element? If so, do you mean the modification did not work?
Regards, Aland Li Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Thursday, September 17, 2009 3:40 AM |
Yeah, I have two text box's I found the "name" in the HTML, but the button does not have a name, it has an "id" only from what I can see. and when I tested just the text fields, they didn't change. |
| NintendoZACHERY Thursday, September 17, 2009 3:50 AM |
Hi NintendoZACHERY,
In the code snippet below, "a" is the id, so that you can find it. ask As HtmlElement = doc.All("q") Then you can follow the code below to modify its text: ask.InnerText = "nobugz"
Do you mean that you have done this but the text of the button is not changed?
Regards, Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Thursday, September 17, 2009 3:56 AM |
Hi NintendoZACHERY,
In the code snippet below, "a" is the id, so that you can find it. ask As HtmlElement = doc.All("q") Then you can follow the code below to modify its text: ask.InnerText = "nobugz"
Do you mean that you have done this but the text of the button is not changed?
Regards, Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Thursday, September 17, 2009 3:57 AM |
I'm not wanting to change the text of the button, I wanted to change the text of two text box's then press a button. |
| NintendoZACHERY Thursday, September 17, 2009 3:58 AM |
Hi NintendoZACHERY, If you only have names of the text boxes, you cannot get it as follows if the name is "textBox1": DimtextBox1 As HtmlElement = doc.All( "textBox1") Based on my understanding, we can follow the code snippet below to find it and change its text:
For Each textElement As HtmlElement In doc.GetElementsByTagName("textarea")
If textElement.Name = "textBox1" Then
textElement.InnerText = "hello"
End If
Next
Let me know if this does not help. Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Thursday, September 17, 2009 4:07 AM |
I figured out that this want the problem. You see I have a button that starts a timer, and I have a bool value, every time the counter ticks it checks if the bool is true, if it is, the browser will navigate to a URL givin a ID value
Then I have a document Completed funtion on the browser, if the document gets completed, it writes stuff into the two text box's and clicks the button, then changes the ID value, and changed the bool to true, starting the whole process over until there are no more ID's and everything stops.
The code works perfectly when the web browser is just on the page, and I click a button to write text etc. and then send it. but my method doesn't seem to work. My bjective is to go through the ID's as fast as possible until there are no more.
If you have an idea I can do besides my method, that would be great. |
| NintendoZACHERY Friday, September 18, 2009 1:41 AM |
Hi NintendoZACHERY,
You said: The code works perfectly when the web browser is just on the page, and I click a button to write text etc. and then send it. but my method doesn't seem to work.
Do you mean it works fine on the first page, but has issues on the following pages? Could you provide more information about the issue or what errors you met? It is appreciated if you could provide a code snippet.
By the way, we can call the HtmlDocument.InvokeScript method to invoke a java script function.
This is the document: http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.invokescript.aspx
This is a thread: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/9a1b7743-7089-4d44-82bc-3409c3216856.
Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Friday, September 18, 2009 3:04 AM |
Like, when the web browser is just sitting on a page, I can click a button to change its text values, and press buttons, but my method makes it immpossible to do so for a reason? |
| NintendoZACHERY Saturday, September 19, 2009 9:56 AM |
Hi NintendoZACHERY,
So the method like the code snippet below cannot meet your needs: Dim doc As HtmlDocument = WebBrowser1.Document Dim ask As HtmlElement = doc.All("q") Dim lucky As HtmlElement = doc.All("btnI") ask.InnerText = "nobugz" lucky.InvokeMember("click")
I provide a method to invoke the javascript method. Can it help you?
You said but my method makes it immpossible to do so for a reason? Could you please let me know more about the reason or why the methods we provided cannot meet your needs so that we can be able to provide you more suggestions?
Regards, Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Monday, September 21, 2009 2:26 AM |
Ok, my problem is, I have a list of ID's User ID's to be specific. When I press a button, a browser will Private Message the first ID. Then after its done, it will move on to a new ID.
I made this go as fast as possible my using the Document Completed funtion. But it seems the messages don't send or anything. Thats all. |
| NintendoZACHERY Monday, September 21, 2009 2:29 AM |
Hi NintendoZACHERY,
Could you please provide a piece of html content and the code snippet you are executing now?
Regards, Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Monday, September 21, 2009 2:38 AM |
Hi,
We are changing the issue type to “General Discussion�because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question�by opening the Options list at the top of the post window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Tuesday, September 22, 2009 10:04 AM |