Solved

c#: How to get links from webBrowser1 DOM

Posted on 2011-03-20
3
676 Views
Last Modified: 2012-06-27
I added webBrowser1 to a form in C# Express and coded it as shown below.

Next I added a breakpoint and inspected the DOM using the locals panel and the following path:

this.webBrowser1.Document.DomDocument

There is an object called "links":

this.webBrowser1.Document.DomDocument.links

and its value is:

{System._ComObject}

How would I extract these links and put them in an array?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DOM
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.webBrowser1.Url = new Uri("http://www.experts-exchange.com");
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //CODE TO GET LINKS
        }
    }
}

Open in new window

0
Comment
Question by:AdrianSmithUK
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35175900
Well the Document property itself has a member called "Links." The only problem I see is that it is a collection of HtmlElement objects, and not something more link-specific. There may be a more specific member you can cast it to in order to get information more relevant to links, with respect to property names, but I'm not sure what that would be. Instead, below is an example of how you could extract the href from each link:
List<string> hrefs = new List<string>();

foreach (HtmlElement link in this.webBrowser1.Document.Links)
{
    hrefs.Add(link.GetAttribute("href"));
}

Open in new window

0
 

Author Closing Comment

by:AdrianSmithUK
ID: 35176018
A very elegant solution.

Many thanks.

Adrian Smith
London
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35176338
NP. Glad to help  = )
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Check my code out(2) 2 54
Performance  Html.BeginForm vs jQuery 3 51
About delegates in c sharp 3 38
Error in script 11 48
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now