this is the html code
<span class="vi-atw-txt">Add to watch list</span>
I use following code to click it
webBrowser1.Document.GetElementById("vi-atw-txt").InvokeMember("click");
but it showing a nullreferenceexception...
what should i do?
this is the html code
<span class="vi-atw-txt">Add to watch list</span>
I use following code to click it
webBrowser1.Document.GetElementById("vi-atw-txt").InvokeMember("click");
but it showing a nullreferenceexception...
what should i do?
You are passing a classname into GetElementById which will not work. C# does not offer a method to search by class name directly, but you can try the solution in How to getelement by class? except with a span as the tag to search for, and then fire the click when your class matches.
var links = webBrowser1.Document.GetElementsByTagName("span");
foreach (HtmlElement link in links)
{
if (link.GetAttribute("className") == "vi-atw-txt")
{
link.InvokeMember("click");
}
}
What this code will do is create a collection of all spans, and then search through those spans until it finds a span with a classname of "vi-atw-txt". Once that is found, it will call click on that span.
To use the method getElementById you would have to add an id to the span tag:
<span id="someid" class="vi-atw-txt">Add to watch list</span>
Then:
webBrowser1.Document.GetElementById("someid").InvokeMember("click");