gpt4 book ai didi

vb.net - InvalidCastException 与 WebBrowser.IsBusy 或 ReadyState (VB .NET)

转载 作者:太空狗 更新时间:2023-10-29 20:41:55 31 4
gpt4 key购买 nike

我正在尝试作为我的另一个问题 (Automate website log-in and form filling?) 的答案而建议的方法,并注意到一些奇怪的事情。

上述问题的答案是使用一系列 javascript 调用作为 URL,以填写网络表单并提交。我一直试图在 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 事件等待浏览到嵌套 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

奇怪的是,在等待循环中检查 ReadyState(或 IsBusy,就此而言)有时会抛出 InvalidCastException。大概调用这些不是线程安全的?我不知道。如果我将有问题的调用放在 Try block 中,等待循环就无法工作。事实上,异常似乎“持续存在”以搞砸一切,因为即使使用 try block 逐步执行代码,Visual Studio 也会卡住 10 到 20 秒(没有 try block 也是如此)。

有什么想法吗?

最佳答案

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

关于vb.net - InvalidCastException 与 WebBrowser.IsBusy 或 ReadyState (VB .NET),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18232441/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com