Solved

c#: How to get links from webBrowser1 DOM

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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