I am trying to use WebBrowser
since I need to interact with some elements created via JS and not present in the original response.
I have read other questions where the event is not being fired because the single-threaded program is waiting for a read key or something similar, and thus the event cannot be fired.
However, in my case, it is just finishing the program and the Client_DocumentCompleted
method is never entered.
[STAThread]
static void Main(string[] args)
{
Helper();
}
static void Helper()
{
WebBrowser client = new WebBrowser();
client.DocumentCompleted += Client_DocumentCompleted;
client.AllowNavigation = true;
client.Navigate("https://www.google.com/");
}
private static void Client_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser client = (WebBrowser)sender;
while (client.ReadyState != WebBrowserReadyState.Complete)
{
Console.WriteLine(client.ReadyState);
}
string htmlCode = client.Document.ToString();
Console.Write(htmlCode);
Console.ReadKey();
}
I tried doing the WebBrowser
interaction in a separate thread without success.
var t = new Thread(Helper);
t.SetApartmentState(ApartmentState.STA);
t.Start();
Edit: Updated to include the [STAThread] attribute which was already present in my solution