通过vb.net编程单击html按钮

通过vb.net编程单击html按钮,vb.net,Vb.net,我必须按程序点击网页第三页的HTML按钮。按钮没有id。它只有名称类型和值。按钮的HTML代码如下所示 <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='hid

我必须按程序点击网页第三页的HTML按钮。按钮没有id。它只有名称类型和值。按钮的HTML代码如下所示

<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>

但是我不能点击它。我正在使用vb.NET2008平台。有人能告诉我单击它的解决方案吗?

听起来好像您正在尝试执行客户端操作-在服务器上操作时单击按钮(VB代码)。这是行不通的


因此,可以使用Javascript
document.form1.submit()
或调用连接到按钮
btnsub的VB sub。单击
(或任何您命名的按钮)。

尝试在表单上调用submit,而不是单击输入

编辑:哦,HTMLElementCollection不实现通用IEnumerable。请尝试以下方法:

Dim l_forms = WebBrowser1.Document.GetElementsByTagName("form")
If l_forms.Count > 0 Then
  l_forms.Item(0).InvokeMember("submit")
End If

解决方案:

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
WebBrowser1.导航(“UrlHere!”)
“等这一页老了再说
执行WebBrowser1.ReadyState时执行WebBrowserReadyState.Complete
Application.DoEvents()
环
WebBrowser1.Document.Body.InnerHtml=Replace(WebBrowser1.Document.Body.InnerHtml,“NAME='btnSub'”,“NAME='btnSub'id='btnSub'”)将id插入您的按钮
WebBrowser1.Document.GetElementById(“btnSub”).InvokeMember(“单击”)单击btnSub

)()

我花了很长时间试图找到这个问题的答案。我从未意识到可以使用javascript调用单击。一旦我读到解决方案变得非常简单:

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
感谢Dan Tao提供的随机数字:


测试和工作

你在做自动化测试吗?您是否想过使用Selenium这样的框架来自动化您的测试用例?这是asp.net吗?您将代码放在这个示例中的何处?是的,我想自动创建一个网站。我正在尝试找出这是vbscript还是服务器端代码。只是为两者提出了一个可能的解决方案。但尼科曼已经为你指明了正确的方向。祝你好运,谢谢你。还有其他的解决办法吗?@ChauhdryKing-因为我没有你完整的代码,所以我无法测试任何理论。我认为你在这方面已经有了一些很好的答案,所以你要么需要修改你的问题,要么自己动手解决,直到你解决问题为止。抱歉,您必须比“此代码不起作用”更具体
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
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
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