使用 GeckoWebBrowser 单击 HTML 按钮下载文件

Download a file on HTML button click using GeckoWebBrowser

VB.NET 项目中,使用 GeckoWebBrowser 控件 (v45.0.34.0),我正在尝试 下载单击 html 按钮 上的网页中的文件。我设法加载了页面,找到了按钮,但是当我的代码 "clicks" 出现在上面时,我什么也得不到!!!我是否必须为此创建任何 "extra" 事件?是不是像WebBrowser控制一样工作?

Imports Gecko

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GeckoWebBrowser1.Navigate("http://mywebpage.com/download.html")
    End Sub

    Private Sub GeckoWebBrowser1_DocumentCompleted(sender As Object, e As Events.GeckoDocumentCompletedEventArgs) Handles GeckoWebBrowser1.DocumentCompleted
        Dim _Elements As GeckoElementCollection = GeckoWebBrowser1.Document.GetElementsByTagName("input")
        For Each _Element As GeckoHtmlElement In _Elements
            Dim _src As String = _Element.GetAttribute("src")
            Dim _value As String = _Element.GetAttribute("value")
            If _src IsNot Nothing AndAlso _src <> "" AndAlso _src.Contains("button-download.gif") Then
                _Element.Click()
            ElseIf _value IsNot Nothing AndAlso _value <> "" AndAlso _value.Contains("Download") Then
                _Element.Click()
        End If
        Next
    End Sub

End Class

编辑:

这是我尝试集成的页面 HTML 代码的一部分。请注意,如果我对 .NET WebBrowser 控件执行相同的操作,它会起作用!!!

<td>
    <div align="center">
        <!--<a rel="nofollow" href="getSub-tkMTUyMTgzNTIzMjQyNjI1MDYyNDI2Mjk2Mzk0OTg5.html" class="style55ws"><img src="http://***.com/button-download.gif" alt="down" width="200" height="58" /></a>-->
        <input type="hidden" name="id" value="tkMTUyMTgzNTIzMjQyNjI1MDYyNDI2Mjk2Mzk0OTg5">
        <input type="image" src="http://***.com/button-download.gif" width="200px" height="58px" alt="submit">
    </div>
</td>

实际上,下载文件有点复杂。我不能 post 我自己的 VB 代码(我做 C#)示例/解决方案,但是工作示例在下面的 link 中:

简而言之,您要做的就是提供下载管理器的实现,并自行处理文件持久化、进度等。但是,它主要是样板代码,您应该能够根据示例弄清楚要做什么:

代码来自这个 bitbucket 问题,我正在 post 处理它以防 link 失效: https://bitbucket.org/geckofx/geckofx-45.0/issues/15/downloading-files-with-geckofx-45

Imports Gecko
Imports System.IO
Imports System.Net
Imports System.Text.RegularExpressions
' SU WINDOWS XP INSTALLARE Visual C++ Redistributable per Visual Studio 2013 vcredist_x86.exe

Public Class Form1
    'ask for save
    'user_pref("browser.download.useDownloadDir", false);
    '
    'set download folder
    'user_pref("browser.download.dir", "C:\Download");

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Browser.NoDefaultContextMenu = True
        Browser.Navigate("www.google.it")

        AddHandler Gecko.LauncherDialog.Download, AddressOf LauncherDialog_Download

        TextBox1.Text = "https://download.microsoft.com/download/7/2/9/7290EA05-DC56-4BED-9400-138C5701F174/WS2016LicensingDatasheet.pdf"

        'TEST EDIT USER AGENT
        Dim sUserAgent As String = "Mozilla/5.0 (Windows; U; Windows NT 6.1; pl; rv:1.9.1) Gecko/20090624 Firefox/3.5 (.NET CLR 3.5.30729)"
        Gecko.GeckoPreferences.User("general.useragent.override") = sUserAgent

        'THIS DISABLE THE PDF PLUGIN THAT OPEN THE PDF ON BROWSER
        Gecko.GeckoPreferences.User("plugin.disable_full_page_plugin_for_types") = "application/pdf"
    End Sub

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            Browser.Navigate(TextBox1.Text)
        End If
    End Sub

    Dim DownloadMan As nsIDownloadManager = Nothing
    Private Sub LauncherDialog_Download(sender As Object, e As Gecko.LauncherDialogEvent)
        Dim objTarget As nsILocalFile = Xpcom.CreateInstance(Of nsILocalFile)("@mozilla.org/file/local;1")

        Using tmp As New nsAString(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + vbTab & "emp.tmp")
            objTarget.InitWithPath(tmp)
        End Using

        Dim source As nsIURI = IOService.CreateNsIUri(e.Url)
        Dim dest As nsIURI = IOService.CreateNsIUri(New Uri(Application.StartupPath & "\download\" & e.Filename).AbsoluteUri)
        Dim t As nsAStringBase = DirectCast(New nsAString(System.IO.Path.GetFileName(e.Filename)), nsAStringBase)

        Dim persist As nsIWebBrowserPersist = Xpcom.CreateInstance(Of nsIWebBrowserPersist)("@mozilla.org/embedding/browser/nsWebBrowserPersist;1")
        Dim nst As nsITransfer = Xpcom.CreateInstance(Of nsITransfer)("@mozilla.org/transfer;1")
        Dim m As New MyGekoClass
        m.Init(source, dest, t, e.Mime, 0, Nothing, persist, False)
        nst = m
        If nst IsNot Nothing Then

            persist.SetPersistFlagsAttribute(2 Or 32 Or 16384)
            persist.SetProgressListenerAttribute(DirectCast(nst, nsIWebProgressListener))
            persist.SaveURI(source, Nothing, Nothing, CUInt(Gecko.nsIHttpChannelConsts.REFERRER_POLICY_NO_REFERRER), Nothing, Nothing, _
                DirectCast(dest, nsISupports), Nothing)
        End If
    End Sub
    Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
        target = value
        Return target
    End Function

    Private Sub Browser_ObserveHttpModifyRequest(sender As Object, e As GeckoObserveHttpModifyRequestEventArgs) Handles Browser.ObserveHttpModifyRequest
        'here you can stop the download of same extension by using e.Cancel=True
        Dim str = e.Uri.ToString
        If str.Substring(str.Length - 4) = ".pdf" Then
            'e.Cancel = True
        End If
    End Sub

    Private Sub cmdGo_Click(sender As Object, e As EventArgs) Handles cmdGo.Click
        Browser.Navigate(TextBox1.Text)
    End Sub

End Class
Class MyGekoClass
    Implements nsITransfer

    Public Sub Init(aSource As nsIURI, aTarget As nsIURI, aDisplayName As nsAStringBase, aMIMEInfo As nsIMIMEInfo, startTime As Long, aTempFile As nsIFile, aCancelable As nsICancelable, aIsPrivate As Boolean) Implements nsITransfer.Init

    End Sub

    Public Sub OnLocationChange(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aLocation As nsIURI, aFlags As UInteger) Implements nsITransfer.OnLocationChange

    End Sub

    Public Sub OnProgressChange(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aCurSelfProgress As Integer, aMaxSelfProgress As Integer, aCurTotalProgress As Integer, aMaxTotalProgress As Integer) Implements nsITransfer.OnProgressChange

    End Sub

    Public Sub OnProgressChange64(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aCurSelfProgress As Long, aMaxSelfProgress As Long, aCurTotalProgress As Long, aMaxTotalProgress As Long) Implements nsITransfer.OnProgressChange64

    End Sub

    Public Function OnRefreshAttempted(aWebProgress As nsIWebProgress, aRefreshURI As nsIURI, aMillis As Integer, aSameURI As Boolean) As Boolean Implements nsITransfer.OnRefreshAttempted

    End Function

    Public Sub OnSecurityChange(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aState As UInteger) Implements nsITransfer.OnSecurityChange

    End Sub

    Public Sub OnStateChange(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aStateFlags As UInteger, aStatus As Integer) Implements nsITransfer.OnStateChange

    End Sub

    Public Sub OnStatusChange(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aStatus As Integer, aMessage As String) Implements nsITransfer.OnStatusChange

    End Sub

    Public Sub SetRedirects(aRedirects As nsIArray) Implements nsITransfer.SetRedirects

    End Sub

    Public Sub SetSha256Hash(aHash As nsACStringBase) Implements nsITransfer.SetSha256Hash

    End Sub

    Public Sub SetSignatureInfo(aSignatureInfo As nsIArray) Implements nsITransfer.SetSignatureInfo

    End Sub

    Public Sub OnLocationChange1(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aLocation As nsIURI, aFlags As UInteger) Implements nsIWebProgressListener.OnLocationChange

    End Sub

    Public Sub OnProgressChange1(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aCurSelfProgress As Integer, aMaxSelfProgress As Integer, aCurTotalProgress As Integer, aMaxTotalProgress As Integer) Implements nsIWebProgressListener.OnProgressChange

    End Sub

    Public Sub OnSecurityChange1(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aState As UInteger) Implements nsIWebProgressListener.OnSecurityChange

    End Sub

    Public Sub OnStateChange1(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aStateFlags As UInteger, aStatus As Integer) Implements nsIWebProgressListener.OnStateChange

    End Sub

    Public Sub OnStatusChange1(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aStatus As Integer, aMessage As String) Implements nsIWebProgressListener.OnStatusChange

    End Sub

    Public Sub OnLocationChange2(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aLocation As nsIURI, aFlags As UInteger) Implements nsIWebProgressListener2.OnLocationChange

    End Sub

    Public Sub OnProgressChange2(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aCurSelfProgress As Integer, aMaxSelfProgress As Integer, aCurTotalProgress As Integer, aMaxTotalProgress As Integer) Implements nsIWebProgressListener2.OnProgressChange
        MsgBox("PROGRESS: " & aCurSelfProgress)
    End Sub

    Public Sub OnProgressChange641(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aCurSelfProgress As Long, aMaxSelfProgress As Long, aCurTotalProgress As Long, aMaxTotalProgress As Long) Implements nsIWebProgressListener2.OnProgressChange64
        MsgBox("PROGRESS: " & aCurSelfProgress & "/" & aMaxTotalProgress)
    End Sub

    Public Function OnRefreshAttempted1(aWebProgress As nsIWebProgress, aRefreshURI As nsIURI, aMillis As Integer, aSameURI As Boolean) As Boolean Implements nsIWebProgressListener2.OnRefreshAttempted

    End Function

    Public Sub OnSecurityChange2(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aState As UInteger) Implements nsIWebProgressListener2.OnSecurityChange

    End Sub

    Public Sub OnStateChange2(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aStateFlags As UInteger, aStatus As Integer) Implements nsIWebProgressListener2.OnStateChange

    End Sub

    Public Sub OnStatusChange2(aWebProgress As nsIWebProgress, aRequest As nsIRequest, aStatus As Integer, aMessage As String) Implements nsIWebProgressListener2.OnStatusChange

    End Sub

End Class