I’m trying to click a link using this line:
webBrowser1.Document.GetElementsByTagName("a")[i].InvokeMember("click");
But when the element is not present on the page it gives me the error Value of '0' is not valid for 'index'.
I tried testing the element’s presence first:
if (webBrowser1.Document.GetElementsByTagName("a") != null) {...}
But this doesn’t seem to work.
How do I check if the element is present?
Apparently
webBrowser1.Document.GetElementsByTagName("a")yields an empty collection.Your second sample test for
nullbut that is something different.Use the debugger to see
webBrowser1.Document.GetElementsByTagName("a").Count