C# Webbrowser Control -- Get Document Elements After Ajax

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;
}
}
}

Sample Image

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



Leave a reply



Submit