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?

Chauhdry King
  • 11
  • 1
  • 1
  • 8

5 Answers5

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
sloth
  • 99,095
  • 21
  • 171
  • 219
nnm
  • 865
  • 1
  • 8
  • 9
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
JDB
  • 25,172
  • 5
  • 72
  • 123
  • @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. – JDB Jun 21 '12 at 13:24
  • You are going to have to be more specific then "this code is not working" – JDB Jun 26 '12 at 15:16
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!

Community
  • 1
  • 1
nicky
  • 787
  • 2
  • 12
  • 27
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).

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
ZorbaSan
  • 3
  • 2
  • 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. – ZorbaSan Jun 19 '12 at 13:05
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

;)

nnm
  • 865
  • 1
  • 8
  • 9