Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# How to grab html from selection of web browser control

C# How to grab html from selection of web browser control

Scheduled Pinned Locked Moved C#
csharphtmllinqtutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tridip Bhattacharjee from Unknown
    wrote on last edited by
    #1

    I have a winform project where i am using web browser control which load a site. the site has many tabular data which is a html table. user will select large text from web browser control using their mouse. the select may have many data including multiple tabular data which is nothing but a html table. I know how to get text from selection. this is sample code which return text.

    private string GetSelectedText()
    {
    dynamic document = webBrowser1.Document.DomDocument;
    dynamic selection = document.selection;
    dynamic text = selection.createRange().text;
    return (string)text;
    }

    But i need html of selected area on web browser control programmatically. i use a code sample which suppose to return html of selected portion of web page loaded into web browser control.....but no luck. here i am sharing that code which not working as expected. please see my code and tell me how could grab the html content from web browser control of large selection ? here is the code which is not working.

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace HtmlTableParser
    {
    public partial class Form2 : Form
    {
    private WebBrowser webBrowser1;
    public Form2()
    {
    InitializeComponent();

            Button btn = new Button();
            btn.Text = "Test";
            btn.Click += button1\_Click;
            this.Controls.Add(btn);
    
            var panel = new Panel();
            panel.Top = btn.Height + 2;
            panel.Height = this.ClientSize.Height - btn.Height + 2;
            panel.Width = this.ClientSize.Width;
            panel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
    
            webBrowser1 = new WebBrowser();
            webBrowser1.Dock = DockStyle.Fill;
            webBrowser1.Url = new Uri("https://www.sec.gov/Archives/edgar/data/1108134/000110813423000018/bhlb-20230630.htm");
    
            panel.Controls.Add(webBrowser1);
            this.Controls.Add(panel);
    
        }
    
        private void button1\_Click(object sender, EventArgs e)
        {
            TestSelection();
            TestAllTable();
        }
    
        private void TestSelection()
        {
            var domdoc = this.webBrowser1.Document.DomDocument as mshtml.IHTMLDocument2;
            var sel = domdoc.selection;
            var range = sel.createRange();
    
    Richard DeemingR 1 Reply Last reply
    0
    • T Tridip Bhattacharjee from Unknown

      I have a winform project where i am using web browser control which load a site. the site has many tabular data which is a html table. user will select large text from web browser control using their mouse. the select may have many data including multiple tabular data which is nothing but a html table. I know how to get text from selection. this is sample code which return text.

      private string GetSelectedText()
      {
      dynamic document = webBrowser1.Document.DomDocument;
      dynamic selection = document.selection;
      dynamic text = selection.createRange().text;
      return (string)text;
      }

      But i need html of selected area on web browser control programmatically. i use a code sample which suppose to return html of selected portion of web page loaded into web browser control.....but no luck. here i am sharing that code which not working as expected. please see my code and tell me how could grab the html content from web browser control of large selection ? here is the code which is not working.

      using System;
      using System.Collections.Generic;
      using System.Data;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;

      namespace HtmlTableParser
      {
      public partial class Form2 : Form
      {
      private WebBrowser webBrowser1;
      public Form2()
      {
      InitializeComponent();

              Button btn = new Button();
              btn.Text = "Test";
              btn.Click += button1\_Click;
              this.Controls.Add(btn);
      
              var panel = new Panel();
              panel.Top = btn.Height + 2;
              panel.Height = this.ClientSize.Height - btn.Height + 2;
              panel.Width = this.ClientSize.Width;
              panel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
      
              webBrowser1 = new WebBrowser();
              webBrowser1.Dock = DockStyle.Fill;
              webBrowser1.Url = new Uri("https://www.sec.gov/Archives/edgar/data/1108134/000110813423000018/bhlb-20230630.htm");
      
              panel.Controls.Add(webBrowser1);
              this.Controls.Add(panel);
      
          }
      
          private void button1\_Click(object sender, EventArgs e)
          {
              TestSelection();
              TestAllTable();
          }
      
          private void TestSelection()
          {
              var domdoc = this.webBrowser1.Document.DomDocument as mshtml.IHTMLDocument2;
              var sel = domdoc.selection;
              var range = sel.createRange();
      
      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      No new code should be written today using the ancient WebBrowser control. It's an instance of Internet Explorer, and unless you edit the registry on every single computer where your code runs, it's stuck in IE7-compatability mode. Instead, use a modern control such as WebView2[^] (Edge), CefSharp[^] (Chrome), or GeckoFX[^] (Firefox).


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      T 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        No new code should be written today using the ancient WebBrowser control. It's an instance of Internet Explorer, and unless you edit the registry on every single computer where your code runs, it's stuck in IE7-compatability mode. Instead, use a modern control such as WebView2[^] (Edge), CefSharp[^] (Chrome), or GeckoFX[^] (Firefox).


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        T Offline
        T Offline
        Tridip Bhattacharjee from Unknown
        wrote on last edited by
        #3

        WebView not available for .net core 6 winform project. can you suggest any browser control which i can use

        .net core 6 winform project ?

        Thanks

        Richard DeemingR 1 Reply Last reply
        0
        • T Tridip Bhattacharjee from Unknown

          WebView not available for .net core 6 winform project. can you suggest any browser control which i can use

          .net core 6 winform project ?

          Thanks

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          The Microsoft.Web.WebView2 NuGet package[^] supports .NET Core 3.0 or greater. That means it should work fine in .NET 5/6/7/8/etc. Get started with WebView2 in WinForms apps - Microsoft Edge Development | Microsoft Learn[^]


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups