Pressing button in a HTML webpage from VB .Net application

shafi

Member
Joined
Feb 1, 2007
Messages
9
Programming Experience
Beginner
I am trying to automate a test in the lab by writing a small program in VB .Net. I have a power switch which is controlled by a button on a IE webpage to turn on and off the power. This webpage is similar to the a Google IE web page where we have button to start search. I would like to be able to 'click' this button from my VB .Net program.

So far I have come across window messaging (SendMessage) where we can send messages to controls on other applications. I have used a windows spy software (WINSPECTOR) to identify the controls on the IE webpage. But the problem is that I do not this button created by HTML.

Do you know of a way to do this in VB .Net?
 
View the Html page source and find the ID or Name of button element.
VB.NET:
WebBrowser1.Document.GetElementById("btn1").InvokeMember("Click")
[COLOR=darkgreen]'or[/COLOR]
WebBrowser1.Document.All.GetElementsByName("btn1")(0).InvokeMember("Click")
 
Thanks for your response John. Can you tell me what does WebBrowser1 points to? Is it window handler returned by the FindWindowEx fucntion?

Shafi.
 
WebBrowser1 was the webbrowser control on the form. Better to use webbrowser control on form than to automate IE (What would the need be for you?).

By the way, I'm using .Net 2.0 where webbrowser and html functionality is integrated. For you in .Net 1.1 you use the ActiveX browser control and mshtml. shdocvw.dll (Microsoft Internet Controls) mshtml.tlb (Microsoft HTML Object Library)
See for example http://www.eggheadcafe.com/articles/20031027.ASP
 
Let me explain to you what I am trying to do.

The power switch controls power to a device I am testing. This switch has an inbuilt webserver. So when I type in the IP address then it asks me to enter user id and password. After entring this, it shows another webpage where it has two buttons (HTML 'submit' type). By pressing these buttons I can power on or power off the output of this switch.

I was hoping to log in and leave the webpage where it shows the on/off buttons. So that my application can get a handle to this IE window and somehow press these buttons. As I mentioned earlier I created process var and pointed it to the IE process. I used a window spy software to find various controls on that page. It shows various things, but not the buttons that I want to press. The reason could be because these buttons are created by HTML.

I'll try to read up more on the webbrowser control and see if I can accomplish this. Meanwhile, if you know of a way to use FindWindowEx to be able to send message to such buttons, then please let meknow.

Shafi.
 
Why get all complicated and trying to hook another application when you can do it all within your own application? Using the webbrowser control on form give you a tight coupling you don't have when remote operating other processes. I not saying it can't be done, not even saying it is difficult (see), I just say there is no need for you to do it.
 
I agree with you. After reading more on this control and the example I agree that I can accomplish same with webcontrol. This is what I am trying now.

Shafi.
 
Thanks a lot John. I was able to accomplish this in VB .Net by using WebBrowser control in VB .Net. Let me explain what I did to accomplish this so that someone else might benefit later on.

For VB .Net 2003, the Webbrowser control is not present is default toolbox. So we have to go to Project -> Add Reference -> COM tab then select "Microsoft Internet Controls" and "Microsoft HTML Object Library". Then right click on the Toolbox and press 'Add/Remove Items'. Go to 'COM Components' tab and select 'Microsoft Web Browser Control'. This will show 'Microsoft Web Browser' control in your Toolbox.

Add this conrtol to your form and the default name will be AxWebBrowser1. Use following code for example to automatically click the 'Google Search' button:

VB.NET:
Dim doc As mshtml.HTMLDocument
Dim all As mshtml.IHTMLElementCollection
Dim elm As mshtml.IHTMLElement
Dim strName AsString
Dim strId1 AsString
Dim strId2 AsString
Dim strvalue AsString
Dim strType AsString
 
AxWebBrowser1.Navigate("www.google.com")
doc = DirectCast(AxWebBrowser1.Document, mshtml.HTMLDocument)
all = doc.getElementsByTagName("input")
ForEach elm In all
strName = elm.getAttribute("NAME")
If strName = "btnG" Then
msgbox("You pressed Google Search button")
EndIf
If strName = "btnI" Then
msgbox("You pressed Feel Lucky button")
EndIf
 
Next
 
Last edited by a moderator:
I forgot to mention the code that performs click event. Here is it:

VB.NET:
Dim btnG_button As mshtml.IHTMLElement
change the code I mentioned previously as follows:

VB.NET:
If strName = "btnG" Then
btnG_button = elm
EndIf
 
btnG_button.click()

Shafi.
 
Last edited by a moderator:
In the MSHTML object library the Document.All collection doesn't support the GetElementsByName method, it's directly exposed through the HtmlDocument object. Also, the InvokeMember method is not available, instead the element of type HtmlFormElement expose the Click method. So tranlating the code in post 2 to use MSHTML (and late binding) you can do it like this with only one line of code (choose the line either element ID or Name):
VB.NET:
AxWebBrowser1.Document.GetElementById("btn1").Click()
[COLOR=darkgreen]'or[/COLOR]
AxWebBrowser1.Document.GetElementsByName("btn1")(0).Click()
 
Back
Top