C# How to grab html from selection of web browser control
-
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();
-
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();
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
-
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
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
-
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
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