0

我在下面的代码中遇到以下错误;

预期语句结束第 3 行 - 第 39 列 /// 错误 2 语句不能出现在属性正文中。假定属性结束。第 5 行 - 第 1 列 /// 错误 3 名称 'pageready' 未声明。第 28-31-36 行 第 19-9-13 列

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Button1 Code
        WebBrowser1.Document.GetElementById("Email").SetAttribute("value", TextBox1.Text)
        WebBrowser1.Document.GetElementById("Passwd").SetAttribute("value", TextBox2.Text)
        WebBrowser1.Document.GetElementById("signIn").InvokeMember("click")
        WaitForPageLoad()

        'Button2 Code
        For Each acct As HtmlElement In WebBrowser1.Document.GetElementsByTagName("a")
            If acct.GetAttribute("href").Contains("https://accounts.google.com/b/0/PlusPageSignUp") Then
                acct.InvokeMember("click")
                WaitForPageLoad()
            End If
        Next

    End Sub

Private Property pageready As Boolean = False
    End Property

#Region "Page Loading Functions"

    Private Sub WaitForPageLoad()

        AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
        While Not pageready
            Application.DoEvents()
        End While
        pageready = False
    End Sub

    Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
        If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
            pageready = True
            RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
        End If
    End Sub

#End Region


End Class
4

1 回答 1

1

基本上每次浏览器忙时,您都必须等待它返回...

组合所有按钮(由浏览器上的等待分隔)..

将它们组合成这样的东西

Private Sub WhateverButtonName(sender As Object, e As EventArgs) Handles WhateverButtonName.Click

    'Button1 Code
    WebBrowser1.Document.GetElementById("Email").SetAttribute("value", TextBox1.Text)
    WebBrowser1.Document.GetElementById("Passwd").SetAttribute("value", TextBox2.Text)
    WebBrowser1.Document.GetElementById("signIn").InvokeMember("click")
    WaitForPageLoad()

    'Button2 Code
     For Each acct As HtmlElement In WebBrowser1.Document.GetElementsByTagName("a")
     If acct.GetAttribute("href").Contains("https://accounts.google.com/b/0/PlusPageSignUp") Then
          acct.InvokeMember("click")
          WaitForPageLoad()
     End If
     Next



End Sub

另外使用此代码运行等待潜艇(抱歉没有来源,我忘记了我在哪里找到它)

#Region "Page Loading Functions"

Private Sub WaitForPageLoad()
    AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    While Not pageready
        Application.DoEvents()
    End While
    pageready = False
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        pageready = True
        RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    End If
End Sub

#End Region

编辑: 添加完整代码

Public Class Form1

    Private Property pageready As Boolean = False

Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    'Button1 Code
    WebBrowser1.Navigate("https://accounts.google.com/Login")
    WaitForPageLoad()
    WebBrowser1.Document.GetElementById("Email").SetAttribute("value", TextBox1.Text)
    WebBrowser1.Document.GetElementById("Passwd").SetAttribute("value", TextBox2.Text)
    WebBrowser1.Document.GetElementById("signIn").InvokeMember("click")
    WaitForPageLoad()

    'Button2 Code

End Sub

#Region "Page Loading Functions"

Private Sub WaitForPageLoad()

    AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    While Not pageready
        Application.DoEvents()
    End While
    pageready = False
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        pageready = True
        RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    End If
End Sub

#End Region


End Class

更新

这正是您应该看到的 - 波浪线(我将文本框替换为要测试的实际值)。我在Form2中重新做了我的。您可以为您将 Form2 重命名为 Form1。

在此处输入图像描述

于 2013-09-12T16:49:08.353 回答