【问题标题】:addhandler ie.Documentcompleted , New WebBrowserDocumentCompletedEventHandler(AddressOf DocumentCompleteIE) erroraddhandler ie.Documentcompleted , New WebBrowserDocumentCompletedEventHandler(AddressOf DocumentCompleteIE) 错误
【发布时间】:2019-08-13 11:22:29
【问题描述】:

AddHandler ie.DocumentCompleted 给出 DocumentCompleted' 不是 “对象”事件

如果我输入 ie.DocumentComplete 我得到 ​​p>

“WebBrowserDocumentCompletedEventHandler”类型的值不能 转换为“DWebBrowserEvents2_DocumentCompleteEventHandler”

我遵循了网络上的几个示例并不断收到这些错误。好像它不是代码问题,而是我缺少参考。

Public Class Form1
    Dim myHTMLDoc As HtmlDocument
    Dim myhtmlelement As HtmlElementCollection
    Dim ie As WebBrowser
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ieurl As String
        ieurl = "http://companyurl.com"
        AddHandler ie.DocumentComplete, AddressOf DocumentCompleteIE
        ie.Navigate(ieurl)
        ie.Visible = True

    End Sub
    Private Sub DocumentCompleteIE(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
        MessageBox.Show("completed - " & ie.LocationURL)
        ie.Document.GetElementById("Login").SetAttribute("value", "x123456")
        ie.Document.GetElementById("Pwd").SetAttribute("value", "123456")
        ie.Document.GetElementById("Btn").InvokeMember("click")
    End Sub
End Class

如果我开始输入 ie.Do(在 addhandler 之后),我会看到 DocumentComplete 作为选项,并且 不是 DocumentCompleted,我觉得这很奇怪,因为我在网上到处看到的代码都使用 DocumentCompleted!

【问题讨论】:

  • DocumentCompleted 与 WebBrowser 控件有关。您正在直接使用 InternetExplorer ActiveX。请参阅其文档。删除New WebBrowserDocumentCompletedEventHandler
  • 什么表示我直接使用 ActiveX?进口 SHDocVw?我看到我可能应该直接使用 WebBrowser 控件,因为用户不需要 IE 页面,只需要一些数据!
  • 不确定你的意思是删除 New WebBrowserDocumentCompletedEventHandler(AddressOf DocumentCompleteIE)!
  • 1) 这意味着您使用的是InternetExplorer object,而不是其托管的 WebBrowser 控件包装器。您正在混合属于两个不同对象的方法。您发布的代码只是尝试自动登录、填写和激活 WebForm。为此使用 WebBrowser 控件:简单得多,包袱少,结果相同。 2)AddHandler [WebBrowser].DocumentCompleted, AddressOf DocumentCompleteIE
  • ie = New WebBrowser。这:Dim ie As WebBrowser 定义了一个类型,它不创建实例。

标签: html vb.net internet-explorer


【解决方案1】:

如果您想使用网络浏览器控件加载网页并填写信息并单击按钮,则可以参考下面的示例。

Public Class Form1
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'this is to load the site in web browser control.
        Me.WebBrowser1.Navigate("http://localhost/login_form.html")
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        ' this is to enter values in the textboxes and click the button in web browser control.
        Dim usernameTextBox As HtmlElement = Me.WebBrowser1.Document.All.Item("uname")
        usernameTextBox.InnerText = "x123456"

        Dim passwordTextBox As HtmlElement = Me.WebBrowser1.Document.All.Item("psw")
        passwordTextBox.InnerText = "123456"

        Me.WebBrowser1.Document.GetElementById("signIn").InvokeMember("click")
    End Sub


End Class

输出:

您也可以使用 WebBrowser1_DocumentCompleted 事件来输入值。我也测试过它,它工作正常。要使用它,您只需从 Button3_Click 事件中复制代码并将其粘贴到 WebBrowser1_DocumentCompleted 事件中。

如果你想打开IE浏览器并执行与上面相同的操作,可以参考下面的代码示例。

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'this is to open IE browser and load the page and entering the values and click the button.
        Dim TheBrowser As Object = CreateObject("InternetExplorer.Application")
        TheBrowser.Visible = True
        TheBrowser.Navigate("http://localhost/login_form.html")
        Do Until TheBrowser.ReadyState = 4 'PAGE_LOADED
        Loop
        TheBrowser.Document.GetElementById("uname").SetAttribute("value", "x123456")
        TheBrowser.Document.GetElementById("psw").SetAttribute("value", "123456")
        TheBrowser.Document.GetElementById("signIn").InvokeMember("click")
    End Sub
End Class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 2012-06-02
    相关资源
    最近更新 更多