Reputation: 653
I'm navigating in the constructor first for testing
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.Navigate("http://www.tapuz.co.il/forums/forumpage/393");
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
Then in the DocumentCompleted event:
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection items = this.webBrowser1.Document.GetElementsByTagName("span");
foreach (HtmlElement item in items)
{
if (item.GetAttribute("className") == "addMessage")
{
webBrowser1.Document.GetElementById("onclick").InvokeMember("click");
}
}
}
When i make inspect element on the addMessage button i see:
<span class="addMessage" onclick="location='http://www.tapuz.co.il/forums/addmsg/393/טבע_ומזג_אוויר/מזג_האוויר'"> | הוספת הודעה</span>
But i'm getting null on the line:
webBrowser1.Document.GetElementById("onclick").InvokeMember("click");
What i want to do is to click on the addMessage button.
Upvotes: 2
Views: 1201
Reputation: 1512
you need to change
webBrowser1.Document.GetElementById("onclick").InvokeMember("click");
to
item.InvokeMember("Click");
item is already the element you want to have so you just need to invoke the member click on it.
HtmlElementCollection elc = this.WebBrowserWindow.Document.GetElementsByTagName("button");
foreach (HtmlElement el in elc)
{
if (el.GetAttribute("type").Equals("submit"))
{
el.InvokeMember("Click");
break;
}
}
Upvotes: 1