gpt4 book ai didi

vb.net - 带有 WebBrowser.IsBusy 或 ReadyState 的 InvalidCastException (VB .NET)

转载 作者:行者123 更新时间:2023-12-02 09:03:44 25 4
gpt4 key购买 nike

我正在尝试使用建议作为我的另一个问题( 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 事件等待浏览到嵌套 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 - 带有 WebBrowser.IsBusy 或 ReadyState 的 InvalidCastException (VB .NET),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1912678/

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