Reputation: 105
I'm having a problem when trying to extract hyperlinks from web document!
The approach I'm trying to use is shown below:
HtmlElementCollection ht = wb.Document.Links;
foreach (HtmlElement item in ht)
{
if (item.GetAttribute("href").Contains("name"))
{
linkList.Add(item.GetAttribute("href"));
}
}
When executing this code I get error "Specified cast is not valid." I guess the problem is in the fact, that method executing this code is called on a separate thread than webbrowser. On the same thread i have no problem calling the method.
Upvotes: 0
Views: 234
Reputation: 105
The solution I found is placing the "link getting code" in separate method and invoking the method, on the main thread (where browser is running).
BeginInvoke(new MethodInvoker(delegate() { getUsers(webBrowser1, linkList); }));
Upvotes: 1
Reputation: 163
You can try this code
HtmlElementCollection hc = webBrowser1.Document.GetElementsByTagName("a");
for (int i = 0; i < hc.Count; i++)
{
if (hc[i].GetAttribute("href") == "name")
listBox1.Items.Add(hc[i].InnerHtml);// Or InnerText
}
Upvotes: 1