1

I have to click a HTML button programatically which is on the 3rd page of the website . The button is without id. It has just name type and value . The HTML code of the button is given below

<FORM NAME='form1' METHOD='post' action='/dflogin.php'>
  <INPUT TYPE='hidden' NAME='txtId' value='E712050-15'>
  <INPUT TYPE='hidden' NAME='txtassId' value='1'><INPUT TYPE='hidden' NAME='txtPsw'  value='HH29'>
  <INPUT TYPE='hidden' NAME='txtLog' value='0'><h6 align='right'>
  <INPUT TYPE='SUBMIT' NAME='btnSub' value='Next' style='background-color:#009900; color:#fff;'></h6>
</FORM>

i am using the following code to click it

For Each webpageelement As HtmlElement In allButtons

        If webpageelement.GetAttribute("value") =  "Start" Then

            webpageelement.InvokeMember("click")

        End If

Next

But i cant able to click it . I am using the vb.net 2008 platform. Can anyone tell me the solution to click it?

4
  • Are you doing test automation? Have you thought of using a framework like Selenium to automate your test cases? Commented Jun 19, 2012 at 12:39
  • Is this asp.net? Where are you putting the code in this sample? Commented Jun 19, 2012 at 12:39
  • yes i want to automate a website Commented Jun 19, 2012 at 12:49
  • possible duplicate of Clicking HTML button in vb.net Commented Jun 19, 2012 at 17:07

5 Answers 5

1

Try invoking Submit on the Form rather than click on the Input.

EDIT: Oops, HTMLElementCollection does not implement the generic IEnumerable. Try this instead:

Dim l_forms = WebBrowser1.Document.GetElementsByTagName("form")
If l_forms.Count > 0 Then
  l_forms.Item(0).InvokeMember("submit")
End If
Sign up to request clarification or add additional context in comments.

2 Comments

@ChauhdryKing - As I don't have access to your complete code, I can't test any theories. I think you've got some good answers on here, so either you'll need to revise your question or plug away at it yourself until you figure out the problem. Sorry.
You are going to have to be more specific then "this code is not working"
1
Dim Elems As HtmlElementCollection
Dim WebOC As WebBrowser = WebBrowser1
Elems = WebOC.Document.GetElementsByTagName("input")
For Each elem As HtmlElement In Elems
    elem.InvokeMember("click")
Next

Comments

1

I spent quite a while trying to find an answer to this. I never realized that you can invoke a click using javascript. Once I read that the solution becomes very easy:

Public Function clickbyid(ByVal id)

    If TheBrowser.Document.GetElementById(id) IsNot Nothing Then
        Dim Headers As String = "" 'insert headers if you want to

        TheBrowser.Navigate("javascript:document.getElementById('" & id & "').click();", "_self", Nothing, Headers)

        'This keeps the function active until the browser has finished loading
        Do While TheBrowser.ReadyState <> WebBrowserReadyState.Complete
            Application.DoEvents()
        Loop
        Return 1
    Else

        MessageBox.Show("Could not find link by id" & id)
        Return Nothing
    End If

End Function

You may have to change "TheBrowser" to "WebBrowser1". If you don't want to return a result then simply change it to public sub and remove the return statements.

If the htmlelement does not have an id then use the following functions to add one

Public Function clickbyelement(ByVal theButton As HtmlElement)

    Try

        'Generate a unique id to identify the element
        Dim randomID = "vbAdded" & GetRandom(10000, 100000).ToString
        'check to make sure the ID does not already exist
        While TheBrowser.Document.GetElementById(randomID) IsNot Nothing
            randomID = "vbAdded" & GetRandom(10000, 100000).ToString
        End While
        'add the ID to the element
        theButton.SetAttribute("id", randomID)
        'click
        clickbyid(randomID)
        Return True

    Catch ex As Exception

        Return False

    End Try

End Function

Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
    ' by making Generator static, we preserve the same instance '
    ' (i.e., do not create new instances with the same seed over and over) '
    ' between calls '
    Static Generator As System.Random = New System.Random()
    Return Generator.Next(Min, Max)
End Function

Thanks to Dan Tao for the random number: https://stackoverflow.com/a/2677819/381273

Tested and working!

Comments

0

It sounds like you are trying to do a client-side action - click the button when operating on the server (VB code). This will not work.

So, either use Javascript document.form1.submit() or call the VB sub that is connected to the button btnsub.click (or whatever you named it).

1 Comment

am trying to find out if this is vbscript, or server side code. Merely suggesting a possible solution for both. But NikoMman is already pointing you in the right direction. good luck.
0

The Solution:

WebBrowser1.Navigate("UrlHere!")

'wait till the page is laoded

Do While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
Loop

WebBrowser1.Document.Body.InnerHtml = Replace(WebBrowser1.Document.Body.InnerHtml, "NAME='btnSub'", "NAME='btnSub' id='btnSub'") 'insert id into youre button
WebBrowser1.Document.GetElementById("btnSub").InvokeMember("click") 'click on btnSub

;)

1 Comment

Thank u . Is there any other solution?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.