Click a <button> element in a webBrowser component
-
Tuesday, January 20, 2009 10:40 PM
Hello.I have a WebBrowser component on a form. When the target page is navigated to, I need to click a <button> tag element in the WebBrowser's document. I can get the reference to the button fine, and even verify it's className attribute the following way:if (((HTMLButtonElementClass)(tE.DomElement)).className == "someClass"){//this is the place where I need to click this button. However, ((HTMLButtonElementClass)(tE.DomElement)).click() doesn't works.//using tE.InvokeMember("click"); does nothing either.//What's the catch?}Thanks in advance.
Max Pavlov
All Replies
-
Tuesday, January 20, 2009 10:45 PMModeratorTry tE.RaiseEvent("OnClick") and see if that does it for you.
David Morton - http://blog.davemorton.net/ -
Wednesday, January 21, 2009 8:56 AMNope, doesn't clicks the button. But surely, when I click it in the webBrowser itself, it works it's javascript background code fine. Also, when I look at the page source, I am unable to locate any javascript function linked to a button. It just looks like this:<button class=class1 title="Hello world!"></button>Maybe there is a way to see what is being executed when I click the button in browser? Like some external tool that will trace this even?Thanks!
Max Pavlov -
Wednesday, January 21, 2009 11:11 AMModerator
Check out FireBug or the IE Developer Toolbar. This will allow you to view the properties of the specific button pretty easily, and may give you a clue as to what's going on.
David Morton - http://blog.davemorton.net/- Proposed As Answer by Harry ZhuModerator Thursday, January 22, 2009 6:13 AM
-
Wednesday, January 21, 2009 12:18 PMHello again!
Here is the FireBug properties screenshot.
OnClick it says "function" prototype "undefined".
<img src="http://www.maxpavlov.com/image.axd?picture=buttonInfo.png" alt="" />
Let me know if there is some other info I should look in. Thank you for helping me on this one while you can =)
Max Pavlov -
Thursday, January 22, 2009 6:13 AMModerator
Hi,
You can use:I assumed you have a webBrowser named webBrowser1 and the name of the button is "Option1".HtmlElementCollection hec = webBrowser1.Document.GetElementsByTagName("button"); foreach (HtmlElement he in hec) { if (he.InnerText.Equals("Option1")) { he.InvokeMember("click"); } }
Beside Innertext ,you can also use id,name,location ...to specify the button.
You can also use 'webBrowser1.Document.GetElementById();' to get button if you know the button id.
Best regards,
Harry- Proposed As Answer by Harry ZhuModerator Thursday, January 22, 2009 6:13 AM
- Unproposed As Answer by Max Pavlov Thursday, January 22, 2009 9:36 AM
-
Thursday, January 22, 2009 9:43 AMHello, Harry.
As mentioned in the first post in this thread,
//using tE.InvokeMember("click"); does nothing either.
I am completely able to locate my button in webBrowserDocument and have it's variable as HTMLButtonElementClass. I have access to all the properties, but I don't know how to click it from code.
I have tried clicking the button using 3 of the following approaches:
((HTMLButtonElementClass)(tE.DomElement)).click()
tE.InvokeMember("click"); Where tE is a variable of HTMLElement class
tE.RaiseEvent("OnClick")
None of these above doesn't work.
At this point I need a program that will tell me, what event is being raised, when I actually do click it in the WebBrowser control.
Thanks for the effort, the problem is still there.
Max Pavlov -
Thursday, January 22, 2009 10:00 AMModeratorHi,
Could you show us how do you get the button ?It seems that you just post the code how do you call the button.
Best regards,
Harry

