在一网页内,相关代码如下
    <div id="toolbar" style="margin-top: 3px;">
        <button type="button" id="btnClose" name="btnClose">
            退出</button>
        <button type="button" id="btnSave" name="btnSave">
            保存</button>
        <button type="button" id="btnModify" name="btnModify">
            修改</button>
        <button type="button" id="btnSCustomer" name="btnClose">
            转我客户</button>
        <button type="button" id="btnPrivateCustomer" name="btnClose">
            转公共</button>
              <button type="button" id="btnReturn" name="btnReturn">
            退回</button>
    </div>
我用WebBrowser作程序,目的 是将自动获取按扭,自动点击,当然,我是先设为修改安钮标题作为试验,能实现这个点击也就实现了,但问题是找不到我批定按钮。代码如下
Private Sub Timer1_Timer()     Dim i&
    
                      '' Dim IDoc As HTMLDocument
        Set IDoc = WebBrowser1.document
                  ''' Dim Acollection As IHTMLElementCollection
 '' '' '' '' '' ' '' '' '' '' ''  
''''''''原来代码   Set Acollection = IDoc.All.tags("button")
        Dim s2 As String
        For i = 0 To Acollection.length - 1
           s2 = Acollection.Item(i).Type
           Debug.Print s2If s2 = "button" Then Acollection.Item(i).Value = "222"  ’这是为让我看到效果
If Acollection.Item(i).Name = "word" Then Acollection.Item(i).Value = “这就是我要找的”     Next' '' '' '' '' '' '修改测试'' '' '' '' '' '' '' '' '' '' '' '' '' '' ''‘’‘’‘’‘’‘’‘’‘’‘’‘’‘ '' '' '' '' '' '' '' '' ''
           Set Acollection = IDoc.All.tags("button")
        Dim s3 As String
         Dim s33 As String
        For i = 0 To Acollection.length - 1
           s3 = Acollection.Item(i).Type
            s33 = Acollection.Item(i).Name
   ' Debug.Print s3
         If s3 = "button" Then Acollection.Item(i).Value = "你好呀"  ‘这里结果显示出来了
          
         If s33 = "btnModify" Then
         Acollection.Item(i).Value = "我要找的"
          
         End If
         End If
    '''''' If s33 = "btnModify" Then Acollection.Item(i).Click
       
       Next测试结果是所有按钮都找到了,也就是 所有按钮 都显示“2222222”  或"你好呀"
但就是找不到我指字的那个 修改<button>
 求解如何解决

解决方案 »

  1.   

    你为什么不用ID来查?WEBBROWSER.DOCUMENT.GETELEMENTBYID.btnModify这样不是就查到了?完了直接在执行他的CLICK事件啊。WEBBROWSER.DOCUMENT.GETELEMENTBYID.btnModify.click();
      

  2.   

    Sub 查找修改按钮并单击()
        On Error Resume Next
        With CreateObject("internetexplorer.application")
            .Visible = True
            .Navigate "http://www.abcd1234.com/"    '这里填入网页地址
            Do Until .ReadyState = 4
                DoEvents
            Loop
            .document.All("btnModify").Value = "1234567890"    '标记一下,主要是看看是不是修改按钮
            .document.All("btnModify").Click    '点击,
        End With
    End Sub
      

  3.   

    你的htm标签有 name 属性你才能够通过 s33 = Acollection.Item(i).Name 取得相关的值,如果没有你当然是取不到 Name 这个属性的值,还有,在前台脚本中通常是以 id 属性来使用对象的,name 属性通常是将表单数据提交给服务器脚本时才会用到,所以你改为 s33 = Acollection.Item(i).id 可能会行得通
      

  4.   

    自己解决了:       '测试绑定网页中测试按钮单击事件
            Dim doc As HtmlDocument = webMain.Document
           Dim btnTest As HtmlElement = doc.All("btnTest")
            '委托处理事件的处理程序
            If Not IsNothing(btnTest) Then
                AddHandler btnTest.Click, AddressOf btnTest_Click
           End If Private Sub btnTest_Click(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
            Dim obj As HtmlElement = CType(sender, HtmlElement)
            Debug.Print("接收到 " & obj.Id & " 的 " & e.EventType & " 事件")
        End Sub