【问题标题】:Programatically clicking an html button by vb.net通过 vb.net 以编程方式单击 html 按钮
【发布时间】:2012-06-21 11:56:54
【问题描述】:

我必须以编程方式单击网站第三页上的 HTML 按钮。该按钮没有ID。它只有名称 type 和 value 。按钮的 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>

我正在使用下面的代码来点击它

For Each webpageelement As HtmlElement In allButtons

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

            webpageelement.InvokeMember("click")

        End If

Next

但我无法点击它。我正在使用 vb.net 2008 平台。谁能告诉我点击它的解决方案吗?

【问题讨论】:

  • 你在做自动化测试吗?您是否考虑过使用像 Selenium 这样的框架来自动化您的测试用例?
  • 这是 asp.net 吗?您将代码放在此示例中的什么位置?
  • 是的,我想自动化一个网站
  • Clicking HTML button in vb.net 的可能重复项

标签: vb.net


【解决方案1】:

我花了很长时间试图找到这个问题的答案。我从来没有意识到你可以使用 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

您可能需要将“TheBrowser”更改为“WebBrowser1”。 如果您不想返回结果,只需将其更改为 public sub 并删除 return 语句。

如果html元素没有id那么使用下面的函数添加一个

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

感谢丹涛提供随机数:https://stackoverflow.com/a/2677819/381273

经过测试并且可以正常工作!

【讨论】:

    【解决方案2】:
    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
    

    【讨论】:

      【解决方案3】:

      尝试在表单上调用提交,而不是点击输入。

      编辑:糟糕,HTMLElementCollection 没有实现通用的 IEnumerable。试试这个:

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

      【讨论】:

      • @ChauhdryKing - 由于我无法访问您的完整代码,因此无法测试任何理论。我认为您在这里得到了一些很好的答案,因此您要么需要修改您的问题,要么自己解决问题,直到找出问题所在。对不起。
      • 你必须更具体然后“这个代码不起作用”
      【解决方案4】:

      解决方案:

      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
      

      ;)

      【讨论】:

      • 谢谢你。还有其他解决办法吗?
      【解决方案5】:

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

      所以,要么使用 Javascript document.form1.submit(),要么调用连接到按钮 btnsub.click(或您命名的任何名称)的 VB 子程序。

      【讨论】:

      • 我正在尝试找出这是 vbscript 还是服务器端代码。只是为两者提出可能的解决方案。但 NikoMman 已经为您指明了正确的方向。祝你好运。
      猜你喜欢
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 2017-06-17
      • 2013-05-23
      • 1970-01-01
      • 2022-08-24
      相关资源
      最近更新 更多