Html 检查 Webbrowser 控件 (vb.net) 中的 Document 中是否存在 HTMLElement

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2022070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:48:38  来源:igfitidea点击:

check if HTMLElement exists in Document in webbrowser control (vb.net)

html.netvb.netbrowser

提问by Jonathan.

I am trying to get the HTML inside a HTMLElement which has an id "block". I have tried:

我正在尝试将 HTML 放入一个 id 为“block”的 HTMLElement 中。我试过了:

If webbrowser1.document.getelementbyid("block") isnot nothing then
  MsgBox(webbrowser1.document.getelementbyid("block").innerHTML)
end if

But it keep throwing a NullReferenceException and tells me to check if it null/nothing which is what I'm doing.

但它不断抛出 NullReferenceException 并告诉我检查它是否为空/无,这就是我在做什么。

So how do I check if an element in a HTMLdocument with a certain ID exists?

那么如何检查具有特定 ID 的 HTML 文档中的元素是否存在?

回答by JaredPar

What's likely happening here is that webbrowser1.document is Nothing and that is what's causing the NullReferenceExceptionto be thrown.

这里可能发生的事情是 webbrowser1.document 是 Nothing ,这就是导致NullReferenceException抛出的原因。

Try the following code

试试下面的代码

If webbrowser1.document IsNot Nothing Then
  Dim element = webbrowser1.document.getelementbyid("block")
  if element isNot Nothing Then
    MsgBox(element.innerHTML)
  End if
end if