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

穿精又带淫゛_ 提交于 2019-12-22 11:16:41

问题


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

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.

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


回答1:


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

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


来源:https://stackoverflow.com/questions/2022070/check-if-htmlelement-exists-in-document-in-webbrowser-control-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!