Solved

c#: How to get links from webBrowser1 DOM

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Video Player 12 27
C# ASP.Net MVC Entity Framework: Unable to connect to the SQL database for cache dependency registration. 2 52
C# LINQ ForEach() question 6 54
linq, c# 8 22
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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