Tar
Tar

Reputation: 9045

How to use System.Windows.Controls.WebBrowser without displaying it?

I need look-less web-browser to interact with some web-page (e.g. enter text, click some button, then read some DIV element). How can I accomplish it in WPF? I tried using System.Windows.Controls.WebBrowser, but apparently it must be rendered to interact with:

var b = new WebBrowser();

b.LoadCompleted += (sender, args) =>
{
    // I never get here...
    Trace.WriteLine(args.Content != null ? "Success" : "Failure");
};

b.Navigate("https://www.google.com");

How can I use it in a non-visual way? or is there a better alternative?

Upvotes: 1

Views: 4283

Answers (2)

noseratio
noseratio

Reputation: 61744

The WPF WebBrowser flavor needs a parent window with live Win32 window handle. Instead, you can use the WinForms WebBrowser version, which can exist without a parent window. Just reference System.Windows.Forms.dll assembly in the project. Internally, either version wraps the WebBrowser ActiveX Control.

I posted a few examples of how to do this. Note most of them are console apps, so they create a secondary STA thread to run a message loop for WebBrowser. In WPF, you can use WinForms WebBrowser on the main UI thread.

Upvotes: 1

user1859022
user1859022

Reputation: 2695

if you don't need any java script to be executed you could use WebRequest
(there is an example at the bottom of the MSDN page...)

Upvotes: 0

Related Questions