VB.NET WebBrowser 单击按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5018072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET WebBrowser click on button
提问by Ionut Ungureanu
I am working at a small VB.NETproject which autofill the fields on the Yahoo register page. Is there a way to click on "Check" button and see if the entered ID is OK or not?
我正在一个小型VB.NET项目中工作,该项目会自动填充雅虎注册页面上的字段。有没有办法点击“检查”按钮,看看输入的ID是否正确?
Something like if the entered ID is OK then proceed further with filling the field, if not, try another ID and press "Check" button again.
例如,如果输入的 ID 是正确的,则继续填写该字段,如果不是,请尝试另一个 ID 并再次按“检查”按钮。
回答by John Koerner
The webbrowser control lets you access elements within the webpage and you can invoke methods on them, so something as simple as this will click the button:
webbrowser 控件允许您访问网页中的元素,您可以调用它们上的方法,所以像这样简单的事情将单击按钮:
webBrowser1.Document.All("yidHelperBtn").InvokeMember("click");
回答by GianT971
Add a timer to your application, with an interval of 1000 ms. Here is the code:
向您的应用程序添加一个计时器,间隔为 1000 毫秒。这是代码:
Dim CheckButton, yahooId As HtmlElement
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
Handles WebBrowser1.DocumentCompleted
处理 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
I added a timer because the browser doesn't seem to validate the Enter key while in the WebBrowser1_DocumentCompleted method.
我添加了一个计时器,因为浏览器在 WebBrowser1_DocumentCompleted 方法中似乎没有验证 Enter 键。
With this code, you can know if the id you entered is OK or not. It is not complete, but it's a good beginning, try to understand and adapt it for your needs.
使用此代码,您可以知道您输入的 id 是否正确。它并不完整,但它是一个好的开始,请尝试理解并根据您的需求进行调整。