• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 705
  • Last Modified:

c#: How to get links from webBrowser1 DOM

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
AdrianSmithUK
Asked:
AdrianSmithUK
  • 2
1 Solution
 
käµfm³d 👽Commented:
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
 
AdrianSmithUKAuthor Commented:
A very elegant solution.

Many thanks.

Adrian Smith
London
0
 
käµfm³d 👽Commented:
NP. Glad to help  = )
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now