获取Recaptcha2 Iframe HTML,但在vb.net中使用GeckoFX浏览器始终为EMPTY

时间:2016-02-06 16:17:56

标签: vb.net iframe captcha recaptcha geckofx

我正在使用最新的GeckoFX-45浏览器for vb.net。

我的问题: 我无法获得Iframe的HTML。

这是我的代码:

'Open Recaptcha2 test site
Browser.Navigate("http://patrickhlauke.github.io/recaptcha/")
'Wait for loading (all ways I know)
Browser.NavigateFinishedNotifier.BlockUntilNavigationFinished()
Dim maxTimeouttime As DateTime = DateTime.Now.AddSeconds(4)
While DateTime.Now < maxTimeouttime
System.Windows.Forms.Application.DoEvents()
System.Threading.Thread.Sleep(100)
End While
While Browser.IsBusy()
System.Windows.Forms.Application.DoEvents()
System.Threading.Thread.Sleep(100)
End While   

'Getting the HTML of the Iframe is always empty:
For Each _E As GeckoIFrameElement In Browser.DomDocument.GetElementsByTagName("iframe")
If _E.GetAttribute("title") = "recaptcha widget" Then
Dim html = _E.ContentDocument '-> is empty  
Dim html2  = _E.OuterHtml '-> HTML does not include the content of the Iframe 
Exit For
End If
Next

该网站在浏览器中显示得非常好,并且recaptcha2 iframe已完全加载并准备就绪,但我如何以编程方式访问它?

非常感谢

1 个答案:

答案 0 :(得分:1)

使用ContentWindow.Document代替ContentDocument。 你的代码应该是:

Dim iFr_dom As GeckoDomDocument
Dim iFr_html As String
For Each iFr As GeckoIFrameElement In Browser.DomDocument.GetElementsByTagName("iframe")
    If iFr.GetAttribute("title") = "recaptcha widget" Then
        iFr_dom = iFr.ContentWindow.Document

        'DomDocument has no OuterHtml property so we use
        iFr_html = iFr_dom.GetElementsByTagName("BODY")(0).OuterHtml
        'Or if the above not work, use the code below
        'iFr_html = iFr_doc.GetElementsByTagName("body")(0).OuterHtml
        Exit For
    End If
Next