上述问题的答案是使用一系列javascript调用作为URL来填写Web表单并提交.我一直试图在VB .NET程序中自动执行此操作但没有成功.
我给出的原始示例不起作用,大概是因为您正在等待与WebBrowser控件正在工作的线程相同的线程:
WebBrowser1.Navigate("http://www.google.com") Do While WebBrowser1.IsBusy OrElse WebBrowser1.ReadyState <> WebBrowserReadyState.Complete Threading.Thread.Sleep(1000) Application.DoEvents() Loop WebBrowser1.Navigate("javascript:function%20f(){document.forms[0]['q'].value='stackoverflow';}f();") Threading.Thread.Sleep(2000) 'wait for javascript to run WebBrowser1.Navigate("javascript:document.forms[0].submit()") Threading.Thread.Sleep(2000) 'wait for javascript to run
如果你根本不等,它当然也不起作用.您最初浏览的URL已中断.但有趣的是,你也不能毫不拖延地对javascript调用执行“导航”.
所以我尝试了另外两种方法:使用DocumentCompleted事件等待浏览到nest URL,直到浏览器加载完页面.不幸的是,DocumentCompleted并不总是触发,并且似乎在每个javascript URL之后都没有触发.
我尝试的第二种方法是将等待放在一个单独的线程中:
Private Delegate Sub SetTextDelegate(ByVal TheText As String) Private Sub delSetText(ByVal TheText As String) WebBrowser1.Navigate(TheText) End Sub Private Sub BrowseTo(ByVal URL As String) If WebBrowser1.Invokerequired Then Me.BeginInvoke(New SetTextDelegate(AddressOf delSetText),URL) Else WebBrowser1.Navigate(URL) End If End Sub Private Sub TargetURL() BrowseTo("http://www.google.com") End Sub Private Sub TypeSomethingIn() BrowseTo("javascript:function%20f(){document.forms[0]['g'].value='test';}f();") End Sub Private Sub SubmitForm() BrowseTo("javascript:document.forms[0].submit()") End Sub Private Sub Wait() While True If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then Exit Sub Threading.Thread.Sleep(100) End While End Sub Private Sub AutoBrowse() TargetURL() Wait() TypeSomethingIn() Wait() SubmitForm() End Sub Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click Dim t As Threading.Thread t = New Threading.Thread(AddressOf AutoBrowse) t.Start() End Sub
奇怪的是,在wait循环中检查ReadyState(或IsBusy)有时会抛出InvalidCastException.据推测这些不是线程安全的吗?我不知道.如果我将违规调用置于Try块中,则等待循环无法正常工作.事实上,甚至看起来异常“坚持”将所有内容都搞砸了,因为即使使用try块Visual Studio踩踏代码也会冻结好10到20秒(没有try块也会这样做).
有任何想法吗?
One of the most interesting issues I had experienced and which for I
was not able to find a solution in inet – was problem related to
WebBrowser control. The thing is that when I was trying to access the
Document property of the WebBrowser control instance,I was getting
“Invalid cast exception”. The thing is that the WebBrowser control is
designed to work in one thread. So to fix this you must only check the
Invokerequired property and if it’s value is true,then call the logic
from the delegate,given into browser.Invoke(…) method.