微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

vb.net – 使用WebBrowser.IsBusy或ReadyState(VB .NET)的InvalidCastException

我正在玩这个方法,建议作为我的另一个问题的答案( Automate website log-in and form filling?),并注意到一些好奇的东西.

上述问题的答案是使用一系列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.

Source

原文地址:https://www.jb51.cc/vb/255288.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐