WebBrowser Control — Get Document Elements After AJAX Call — Null Exception
while ((wb.ReadyState != WebBrowserReadyState.Complete)) {
Application.DoEvents();
}
;)
Is it possible to get post javascript reference in WebBrowser?
If you also control the web page that you are viewing within the WebBrowser control, then you could expose JavaScript methods that return what you need and use Document.InvokeScript to get the value from the JavaScript method.
C#:
object value = this.WebBrowser1.Document.InvokeScript("getValue");
JavaScript:
function getValue() {
var editor = iged_getById("<%=WebHtmlEditor1.ClientID %>");
return editor.getText();
}
If you don't have control over the web site, then you could interact with the document of the webpage through the Document property as that is a .NET object representing the HtmlDocument:
http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.aspx
C# Facing problem to read ajax data using web browser control
AJAX is simple GET or POST request.
Using regular Browser dev tools I've found that page sends simple GET request and receive JSON data. JSON can be deserealized or explored via reader.
For JSON parsing i used Newtonsoft.Json
NuGet package
Here's simple example based on WinForms app.
public partial class Form1 : Form
{
private static readonly HttpClient client = new HttpClient();
private async Task<T> GetJsonPageAsync<T>(string url)
{
using (HttpResponseMessage response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead))
{
response.EnsureSuccessStatusCode();
string text = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(text);
}
}
public Form1()
{
InitializeComponent();
ServicePointManager.DefaultConnectionLimit = 10; // to make it faster
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
}
private async void button1_Click(object sender, EventArgs e)
{
try
{
dynamic newsList = await GetJsonPageAsync<dynamic>("https://www.wsj.com/news/types/newsplus?id={%22query%22:%22type:=\\%22NewsPlus\\%22%22,%22db%22:%22wsjie,blog,interactivemedia%22}&type=search_collection");
List<Task<dynamic>> tasks = new List<Task<dynamic>>();
foreach (dynamic item in newsList.collection)
{
tasks.Add(GetJsonPageAsync<dynamic>($"https://www.wsj.com/news/types/newsplus?id={item.id}&type=article"));
}
dynamic[] newsDataList = await Task.WhenAll(tasks);
foreach (dynamic newItem in newsDataList)
{
textBox1.Text += newItem.data.headline + Environment.NewLine;
textBox1.Text += new string('-', 200) + Environment.NewLine;
}
}
catch (Exception ex)
{
textBox1.Text = ex.Message;
}
}
}
UPD: Added fix for .NET Framework 4.5.2
WebBrowser Control blocking ajax
while(webbrowser1.isBusy); and Thread.Sleep(2000); would block the main message pump, and ajax requires a message pump for asynchronous callback. I suggest you to start a timer in DocumentComplete to poll the web page regularly until you think the page is complete. Remember to stop the timer in BeforeNavigate2
Related Topics
Linq to SQL: Multiple Joins on Multiple Columns. Is This Possible
How to Make Smtp Authenticated in C#
Posting JSON to Url via Webclient in C#
Using Image Control in Wpf to Display System.Drawing.Bitmap
Getting Return Value from Stored Procedure in C#
Main Method Code Entirely Inside Try/Catch: Is It Bad Practice
Show Loading Animation During Loading Data in Other Thread
Dbentityvalidationexception - How to Easily Tell What Caused the Error
What Are the Benefits to Marking a Field as 'Readonly' in C#
Programmatically Mouse Click in Another Window
Import a Public Key from Somewhere Else to Cngkey
How to Lock a Table on Read, Using Entity Framework
Refresh Datagridview When Updating Data Source
How to Get the "Friendly" Os Version Name