2011-02-16 73 views
3

我在一个小的VB.NET项目工作,该项目自动填充雅虎注册页面上的字段。有没有办法点击“检查”按钮,看看输入的ID是否正确?VB.NET WebBrowser点击按钮

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

回答

5

WebBrowser控件可以让你在网页中访问元素,你可以调用它们的方法,从而为这个简单的东西会点击该按钮:

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

向您的应用程序添加一个计时器,间隔为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是否正确。它不完整,但这是一个好的开始,试着理解并适应您的需求。

+0

的确,正如约翰说,你也可以更换 'CheckButton.Focus() SendKeys.Send( “{ENTER}”)' 由 WebBrowser1.Document.All( “yidHelperBtn”)InvokeMember( “点击”) 它会做同样的事情。但是,在文档完成后您仍然需要等待1秒 – GianT971 2011-02-16 20:28:22