【问题标题】:VB.NET WebBrowser click on buttonVB.NET WebBrowser 点击按钮
【发布时间】:2011-06-28 10:22:55
【问题描述】:

我正在从事一个小型 VB.NET 项目,该项目会自动填充 Yahoo 注册页面上的字段。有没有办法点击“检查”按钮,看看输入的 ID 是否正常?

如果输入的 ID 正确,则继续填写该字段,如果不是,请尝试另一个 ID 并再次按“检查”按钮。

【问题讨论】:

    标签: vb.net button browser click autofill


    【解决方案1】:

    为您的应用添加一个计时器,间隔为 1000 毫秒。代码如下:

    Dim CheckButton, yahooId As HtmlElement
    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _ 
    

    处理 WebBrowser1.DocumentCompleted

        yahooId = WebBrowser1.Document.GetElementById("yahooid")
        CheckButton = WebBrowser1.Document.GetElementById("yidHelperBtn")
    
        yahooId.InnerText = "testID" 'Replace testID by the ID you want
    
        Timer1.Start() 'Starts the timer: within 1000 ms (1 s). It will execute the Timer1_Tick sub.
    
    End Sub
    
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        CheckButton.Focus() 'Give the check button the focus
        SendKeys.Send("{ENTER}") 'Causes the validation of the check button
        Timer1.Stop() 'Stops the timer 
    End Sub
    

    我添加了一个计时器,因为浏览器在 WebBrowser1_DocumentCompleted 方法中似乎没有验证 Enter 键。

    通过此代码,您可以知道您输入的 id 是否正常。它并不完整,但它是一个好的开始,请尝试理解并根据您的需要进行调整。

    【讨论】:

    • 确实,正如约翰所说,你也可以用 WebBrowser1.Document.All("yidHelperBtn").InvokeMember("点击”)它会做同样的事情;但是,文档完成后,您仍然需要等待 1 秒
    【解决方案2】:

    webbrowser 控件允许您访问网页中的元素,并且您可以在它们上调用方法,因此像这样简单的操作将单击按钮:

    webBrowser1.Document.All("yidHelperBtn").InvokeMember("click");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      • 2014-12-11
      相关资源
      最近更新 更多