Get the Final Generated HTML Source Using C# or Vb.Net

How can I get the sourcecode and put it in my gridview?

This answer can get you started. You'll probably want some code like:

Dim html As String
html = Await WebView2.ExecuteScriptAsync("document.documentElement.outerHTML;")
html = Regex.Unescape(html)

Note the Await bit. ExecuteScriptAsync() is an async function. It returns a Task(Of String) immediately, you need to await it to get the actual string result.

Get HTML Source after JavaScript manipulations

The trick is going to be finding a way to notify the control about whether the JS is done running. You might be able to do that by having the JS set a form element' value (isJSComplete) when it has completed and polling with the web browser control.

Use the following code to check a form value to see if it is ready

MyBrowserControl.document.getElementById('isJSComplete');

Use the following code to pull the HTML from the page.

MyBrowserControl.Document.documentElement.OuterHTML

Better yet, here is an article showing how to wire up JS events to be handled by the WebBrowser control. You could just fire an event when the JS is done and have your code trap that event and then pull the HTML using the above approach.

VB.Net Webview2 How can I get html source code?

I've only just started messing with the WebView2 earlier today as well, and was just looking for this same thing. I did manage to scrape together this solution:

Dim html As String
html = Await WebView2.ExecuteScriptAsync("document.documentElement.outerHTML;")

' The Html comes back with unicode character codes, other escaped characters, and
' wrapped in double quotes, so I'm using this code to clean it up for what I'm doing.
html = Regex.Unescape(html)
html = html.Remove(0, 1)
html = html.Remove(html.Length - 1, 1)

Converted my code from C# to VB on the fly, so hopefully didn't miss any syntax errors.

Is there any way to run asp.net web browser control on server?

See my comment for other links for a response.

How to get rendered html (processed by Javascript) in WebBrowser control?

Here is one solution I found to get to the rendered HTML(DOM) after javascript was run:

Place a WebBrowser control named webBrowser1 on the Form of class Form1.

[Form1.cs[Design]]

Sample Image

Then for code use:

[Form1.cs]

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WebBrowserTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.webBrowser1.ObjectForScripting = new MyScript();
}

private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://localhost:6489/Default.aspx");
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Navigate("javascript: window.external.CallServerSideCode();");
}

[ComVisible(true)]
public class MyScript
{
public void CallServerSideCode()
{
var doc = ((Form1)Application.OpenForms[0]).webBrowser1.Document;
}
}
}
}

Change the webBrowser1.Navigate("http://localhost:6489/Default.aspx") parameter in Form1_Load to the page whose DOM after being processed by javascript you wish to obtain.

You can access the modified DOM in the CallServerSideCode() method, for example:

doc.GetElementById("myDataTable");

Or you can access the rendered HTML like this:

var renderedHtml = doc.GetElementsByTagName("HTML")[0].OuterHtml;

change html just before it leaves the code-behind

Hi I just wanted to share what I came up with after reading the responses to my post and doing some searching online and with reference to this forum post here http://forums.asp.net/t/1146098.aspx/1 and using HtmlAggilityPack http://htmlagilitypack.codeplex.com/wikipage?title=Examples

Here is the code

Imports System.IO
Imports HtmlAgilityPack

Partial Class Default2
Inherits System.Web.UI.Page

Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)

Dim mem As New System.IO.MemoryStream()
Dim twr As New System.IO.StreamWriter(mem)
Dim myWriter As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(twr)
MyBase.Render(myWriter)

myWriter.Flush()
myWriter.Dispose()

Dim strmRdr As New System.IO.StreamReader(mem)
strmRdr.BaseStream.Position = 0
Dim pageContent As String = strmRdr.ReadToEnd()
strmRdr.Dispose()
mem.Dispose()

writer.Write(AlterWithHTMLAGP(pageContent))

End Sub

Function AlterWithHTMLAGP(ByVal pageSource As String) As String

Dim htmlDoc As HtmlDocument = New HtmlDocument()

htmlDoc.OptionFixNestedTags = True

htmlDoc.LoadHtml(pageSource)

Dim newNode As HtmlNode = HtmlNode.CreateNode("<div>" & "someHtml" & "</div>")

Dim body As HtmlNode = htmlDoc.DocumentNode.SelectSingleNode("//body")

body.PrependChild(newNode)

Return htmlDoc.DocumentNode.WriteTo()

End Function

End Class

Extract specific html string from html source code(website) in vb.net

An easier approach for parsing HTML (especially from a source that you don't control) is to use the HTML Agility Pack, which would allow you to do something a little like:

Dim req As WebRequest = WebRequest.Create("https://www.crowdsurge.com/store/index.php?storeid=1056&menu=detail&eventid=41815")
Dim doc As New HtmlDocument()
Using res As WebResponse = req.GetResponse()
doc.Load(res.GetResponseStream())
End Using

Dim nodes = doc.DocumentNode.SelectNodes("//div[@class='ei_value ei_date']")
If nodes IsNot Nothing Then
For Each var node in nodes
MsgBox(node.InnerText)
Next
End IF

(I've assumed Option Infer)



Related Topics



Leave a reply



Submit