Solved

c#: How to get links from webBrowser1 DOM

Posted on 2011-03-20
3
671 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 74

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 74

Expert Comment

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

15 Experts available now in Live!

Get 1:1 Help Now