GetElementById / GetElementsByTagName找不到元素

时间:2014-08-22 17:29:32

标签: html vb.net

请记住,我只对HTML有所了解:

有一个网站我尝试使用WebBrowser进行交互。该网站有一个textarea元素如下:

<textarea name="ctl00$ContentPlaceHolderMain$txtCallDesc" rows="2" cols="20" id="ctl00_ContentPlaceHolderMain_txtCallDesc" tabindex="205" style="width: 100%; height: 80px; font-size: 8pt"></textarea>

根据我的阅读,生成的textarea ID表示它被放置在某种形式的另一种形式中,我不确定这是否是我所在的地方遇到了我的问题。

页面加载后,我在按钮中有类似的内容:

Dim theCol As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("textarea")
For Each curElement As HtmlElement In theCol
    ListBox1.Items.Add(curElement.TagName)
Next

列表中没有任何内容。我还尝试使用“检查元素”收集的文本框的ID。 Chrome的功能:

Dim value As HtmlElement = WebBrowser1.Document.GetElementById("ctl00_ContentPlaceHolderMain_txtCallDesc")
MsgBox(value.GetAttribute("value"))

无论我做什么,我似乎无法让程序认识到文档中有textarea元素。页面的来源很长,以至于在这里向所有人发送垃圾邮件,但是有什么我想知道我应该注意的吗?也许需要先获得另一个元素,然后搜索其中的元素?

修改

我试图获得的元素似乎属于iFrame,但看起来它来自同一个域,所以相同的原始政策不应该发挥作用,不是吗?

<iframe id="mainFrame" width="100%" height="100%" frameborder="0" class="mainFrame" name="Main" src="/Calls/OpenCalls.aspx">

2 个答案:

答案 0 :(得分:1)

使用Get Iframe HTML中显示的代码:

For i = 0 To WebBrowser1.Document.Window.Frames.Count - 1
    Dim frameDoc = WebBrowser1.Document.Window.Frames(i)

    Dim theCol = frameDoc.Document.GetElementsByTagName("textarea")

    For Each curElement As HtmlElement In theCol
        ListBox1.Items.Add(String.Format("TagName: {0} Id:{1}", curElement.TagName, curElement.Id))
    Next

Next

关键部分是使用WebBrowser1.Document.Window.Frames

答案 1 :(得分:0)

您无法直接引用iframe中的元素,因为它们位于另一个文档中。因此,首先获取iframe中的document元素的引用,然后您可以以相同的方式查询它。

Dim frameDoc = WebBrowser1.Document.GetElementById("mainFrame").DomElement.contentWindow.Docume‌​nt

其他你已经知道的......

Dim theCol = frameDoc.GetElementsByTagName("textarea")
For Each curElement In theCol
    ListBox1.Items.Add(curElement.TagName)
Next