我搜索并搜索了代码,我尝试的所有内容都无效。 基本上我需要在运行测试代码之前完全加载WebBrowser ...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebBrowser1.Document.GetElementById("login").SetAttribute("value", TextBox1.Text)
WebBrowser1.Document.GetElementById("passwd").SetAttribute("value", TextBox2.Text)
WebBrowser1.Document.GetElementById("SI").InvokeMember("Click")
Where I need to insert the WaitForPageLoad()
RichTextBox1.Text = WebBrowser1.DocumentText
If InStr(RichTextBox1.Text, "To continue, create an Xbox profile") Then
MsgBox("You do not have an xbox account associated with this gamertag, please log into xbox.com with the account then create an xbox profile.")
Else
MsgBox("nothing")
End If
正如您所看到的,我尝试使用脚本让我登录Xbox.com,它确实有效,但只是一点点。这段代码的过程很快,它没有检查正确的源代码中的字符串说“要继续......”,基本上
WebBrowser1.Document.GetElementById("login").SetAttribute("value", TextBox1.Text)
WebBrowser1.Document.GetElementById("passwd").SetAttribute("value", TextBox2.Text)
WebBrowser1.Document.GetElementById("SI").InvokeMember("Click")
单击它之后,它会单击执行登录过程的按钮,但它必须加载一个全新的页面,问题是它执行下一行代码的方式太快而下一行代码在错误的源代码中搜索该字符串。我需要它来等待加载该页面,然后运行这一行
RichTextBox1.Text = WebBrowser1.DocumentText
将webbrowser的源代码复制到文本框中,然后搜索该字符串。我尝试了一切。我觉得WaitForPageLoad()会很好用,但是我收到错误告诉我它没有声明。有人可以帮忙吗?
答案 0 :(得分:1)
您必须添加DocumentCompleted Event Handler
并触发相应方法中的任何代码。那就是:
Private Sub startBrowser()
AddHandler WebBrower1.DocumentCompleted, AddressOf WebBrowser_DocumentCompleted
WebBrower1.Navigate("http://...")
End Sub
Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
'CALL ALL YOUR CODE FROM HERE
End Sub
----更新整个WEBBROWSER
如果您打开一个新项目并粘贴此代码(并将TextBoxes
/ RichTextBox
添加到您的表单中),那么它可以正常运行:
Public Class Form1
Friend WithEvents webBrowser0 As New WebBrowser
Friend WithEvents tabs As New TabControl
Friend WithEvents tabPage0 As New TabPage
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
startBrowser()
End Sub
Public Sub startBrowser()
Dim url As String = "http://..."
tabs.Controls.Add(tabPage0)
tabPage0.Controls.Add(webBrowser0)
AddHandler webBrowser0.DocumentCompleted, AddressOf WebBrowser_DocumentCompleted
webBrowser0.Navigate(url)
End Sub
Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
webBrowser0.Document.GetElementById("login").SetAttribute("value", TextBox1.Text)
webBrowser0.Document.GetElementById("passwd").SetAttribute("value", TextBox2.Text)
webBrowser0.Document.GetElementById("SI").InvokeMember("Click")
RichTextBox1.Text = webBrowser0.DocumentText
If InStr(RichTextBox1.Text, "To continue, create an Xbox profile") Then
MsgBox("You do not have an xbox account associated with this gamertag, please log into xbox.com with the account then create an xbox profile.")
Else
MsgBox("nothing")
End If
End Sub
End Class
答案 1 :(得分:0)
此代码应该有所帮助。
定义一个名为complete的全局变量并将其设置为false
Dim completed = false
现在,在您的网络浏览器文档中,完整地将此代码放入
Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
completed = true
End Sub
现在您想等到网络浏览器加载页面
While Not completed
End While
你们应该有这样的东西
Public Class WaitForWebBrowser
Dim completed = False
Sub Main()
WebBrowser1.Navigate("http://google.com")
While Not completed
End While
'when the web browser is done complete will be set to true and will exit the while loop and continue your code
End Sub
Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
completed = true
End Sub
End Class
答案 2 :(得分:-1)
For I As Integer = 0 To 500
If MyBrowser.ReadyState = WebBrowserReadyState.Complete Then Exit For
Threading.Thread.Sleep(1)
Application.DoEvents()
Next