I am creating a small test application in which i am trying to fetch the all the hyperlink's details(Anchor Text and url) on the web page. What i have done till now is that i create webbrowser component loaded the page and fetch all <a> tags. I also know that how to print the value of "href" attribute. I am having trouble to get the text on which link is applied (i.e anchor text).

Here is the code which i have done till now.

Uri page = new Uri("http://www.yahoo.com");
webBrowser1.Url = page;
HtmlElementCollection aTags = webBrowser1.Document.GetElementsByTagName("a");
textBox1.Text= aTags[0].GetAttribute("href");

now the textbox1 contains the address of the 1st link. but how to get the anchor text ??

Try these:

string anchorText = aTags[0].InnerHtml;
anchorText = aTags[0].InnerText;

I don't know which one gets you the anchor text because I don't have a compiler at the moment. Can't test the code. Try both and let me know which one worked :D.

Thanks

commented: Solved my problem.. +0

Try these:

string anchorText = aTags[0].InnerHtml;
anchorText = aTags[0].InnerText;

I don't know which one gets you the anchor text because I don't have a compiler at the moment. Can't test the code. Try both and let me know which one worked :D.

Thanks

Thanks Farooq, both techniques are working...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.