无法在iframe div中的元素上拾取id

时间:2015-05-25 21:54:28

标签: vb.net iframe getelementbyid

使用此{Html ...来自http://www.ebay.com/itm/261901470624?ssPageName=STRK:MESELX:IT&_trksid=p3984.m1558.l2649

<div id="ds_div">
Yada Yada Yada.
</div>

我试图用这两种方法捕获它......

方法1.(给我一个空例外)

TextBoxDetails.Text = WebBrowser1.Document.GetElementById("ds_div").InnerText

方法2.(无错误,无输出)

Dim PageElement3 As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("div")
For Each CurElement As HtmlElement In PageElement3
    If (CurElement.GetAttribute("id") = "ds_div") Then
        TextBoxDetails.Text = CurElement.GetAttribute("InnerText")
    End If
Next

还有什么可以尝试的?

2 个答案:

答案 0 :(得分:0)

在我看来,没有这样的&#34; ds_div&#34;元素,也许它是动态创建的,但这段代码无一例外地工作:

    Dim elementId As String = "ds_div"
    Dim PageElement3 As HtmlElement = WebBrowser1.Document.GetElementById(elementId)
    Dim i As Integer = 0
    If Not PageElement3 Is Nothing Then
        TextBoxDetails.Text = PageElement3.InnerHtml
    Else
        MsgBox("No such element " & elementId & "!")
    End If

尝试使用ID&#34; itemInfoLabel&#34;或者&#34; vi-cdown_timeLeft&#34;

答案 1 :(得分:0)

这就是我如何使用它。我捕获了iframe的源代码,并将其发送到我设置为隐藏的另一个浏览器,然后在文档完成后,我能够检索到我需要的项目。

在我的“Aquire Data”按钮下,我有这段代码......

'Get Item Details
Dim myFrames As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("iframe")
For Each el As HtmlElement In myFrames
    If el.GetAttribute("id") = "desc_ifr" Then
        WebBrowser2.Navigate(el.GetAttribute("src"))
    End If
Next

作为我的事件处理程序,我有这段代码......

Private Sub WebBrowser2_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser2.DocumentCompleted
    If Not WebBrowser2.Document Is Nothing Then
        TextBoxDetails.Text = WebBrowser2.Document.GetElementById("ds_div").InnerText
    End If
End Sub