Knyaz.Optimus 2.2.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package Knyaz.Optimus --version 2.2.6
NuGet\Install-Package Knyaz.Optimus -Version 2.2.6
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Knyaz.Optimus" Version="2.2.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Knyaz.Optimus --version 2.2.6
#r "nuget: Knyaz.Optimus, 2.2.6"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Knyaz.Optimus as a Cake Addin
#addin nuget:?package=Knyaz.Optimus&version=2.2.6

// Install Knyaz.Optimus as a Cake Tool
#tool nuget:?package=Knyaz.Optimus&version=2.2.6

Walkthrough the DOM

All methods and properties of accessing the DOM that you have in the browser's JavaScript are available.

using System.Linq;
using Knyaz.Optimus;
using Knyaz.Optimus.Dom.Elements;
using Console = System.Console;

namespace ConsoleApplication1
{
	class Program
	{
		static async void Main(string[] args)
		{
			var engine = new Engine();
			var page = await engine.OpenUrl("http://google.com").Wait();

			Console.WriteLine("The first document child node is: " + engine.Document.FirstChild);
			Console.WriteLine("The first document body child node is: " + engine.Document.Body.FirstChild);
			Console.WriteLine("The first element tag name is: " + engine.Document.ChildNodes.OfType<HtmlElement>().First().TagName);
			Console.WriteLine("Whole document innerHTML length is: " + engine.Document.DocumentElement.InnerHTML.Length);

			Console.ReadKey();
		}
	}
}

Wait for dynamic elements.

There are methods that allow waiting for the appearance of DOM elements, which are created dynamically in scripts.

using System.Linq;
using System.Threading.Tasks;
using Knyaz.Optimus;
using Knyaz.Optimus.TestingTools;

namespace Sample
{
	class Program
	{
		public static async Task GetHtml5ScoreTest()
		{
			var engine = new Engine();

			//Open Html5Test site
			var page = await engine.OpenUrl("https://html5test.com");

			//Wait until it finishes the test of browser and get DOM element with score value.
			var tagWithValue = page.Document.WaitSelector("#score strong").FirstOrDefault();

			//Show result
			System.Console.WriteLine("Score: " + tagWithValue.InnerHTML);
		}
	}
}

Using of proxy

//create web proxy object
var webProxy = new WebProxy(yor_proxy_address, true);
//initialize resource provider with the proxy
var resourceProvider = new ResourceProviderBuilder()
    .Http(http => http.Proxy(webProxy))
    .Build();

var engine = new Engine(resourceProvider);

Open sites with basic authorization

var resourceProvider = new ResourceProviderBuilder()
    .Http(http => http.Basic(login, password))
    .Build();

var engine = new Engine(resourceProvider);

Get the status code of the page

var page = await new Engine().OpenUrl(value);
if (page is HttpPage httpPage && httpPage.HttpStatusCode == HttpStatusCode.Unauthorized)
{
//do something
}

Form submit

Suppose that you have a form, for example this:

<form>
  <input placeholder="Username" id="username" type="text">
  <input placeholder="Password" id="password" type="password">
  <input value="Logon" type="submit" id="clickme">
</form>

Easy way to submit this form is in the code below:

var page = new Engine().OpenUrl("http://yoursite.here").Wait();
((HtmlInputElement)page.Document.GetElementById("Username")).Value = "John";
((HtmlInputElement)page.Document.GetElementById("Password")).Value = "123456";
((HtmlInputElement)page.Document.GetElementById("clickme")).Click();

Entering of text

The previous example shows how to send a simple form. It will work if your site uses standard send mechanisms. But, if the form is burdened with javascript this method may not work. You may need to emulate the manual input of text with the excitation of all the necessary events in the DOM. For this, there is a special extension method - 'EnterText'.

var page = new Engine().OpenUrl("http://yoursite.here").Wait();
((HtmlInputElement)page.Document.GetElementById("Username")).EnterText("John");
((HtmlInputElement)page.Document.GetElementById("Password")).EnterText("123456");
((HtmlInputElement)page.Document.GetElementById("clickme")).Click();

Go to another page

You can call the OpenUrl method as many times as you like. Each time a call is made, a new Document instance will be created, but all cookies will be saved. In this way, you can emulate the user's transition through links within a single site.

var engine = new Engine();
var mainPage = engine.OpenUrl("http://yoursite.here").Wait();
//... Do something, for example, submit the login form on the main page.
var subPage = engine.OpenUrl("http://yoursite.here/someresource").Wait();
// or if you have some 'a' element on the site
engine.OpenUrl(engine.Document.GetElementsByTagName("a").First().GetAttribute("href"));

Files downloading

Here are a few ways to download a file.

Download the file by url

This method is suitable if the page from which you want to download the file contains direct links to these files, for example:

 <a id=download href="file.txt" download>Download</a>

You just need to get the link from the document and load the file in any accessible way, for example:

var engine = new Engine();
var page = engine.OpenUrl("http://testpage.net/").Wait();
var download = page.Document.GetElementById("download");
var href = download.GetAttribute("href");
var fileLink = new Uri(engine.Uri, href);
//you can get cookies using engine.CookieContainer
using (var client = new WebClient())
    System.Console.Write(client.DownloadString(fileLink));

Download the file initiated by window.open call

Some pages may contain the buttons or other elements like this:

<button type="submit" onclick="window.open('file.doc')">Download!</button>

In browser click on such a button leads to downloading the file from the url specified by first argument of function window.open. There is Engine.OnWindowOpen event to handle such calls:

var engine = new Engine();
engine.OnWindowOpen += (url, name, options) => {
   // download the file, from url using code from samples above.
};

var page = engine.OpenUrl("http://site.net").Wait();

var button = page.Document.GetElementById("download") as HtmlElement;
button.Click();

Pre-define JavaScript functions

Some sites may use the JS functions that does not supported by Optimus. Most often, this is an obsolete API parts. You can execute your script that defines missing functions.

var engine = new Engine();
engine.ScriptExecutor.Execute("text/javascript", "function escape(str){return encodeURI(str)};");
var page = await engine.OpenUrl("http://localhost", false);//second parameter should be 'false'.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net452 is compatible.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on Knyaz.Optimus:

Package Downloads
TAF.WebSupport.HeadlessBrowser

Headless browser for TAF web testing.

Arfilon.Ratchet

Package Description

Knyaz.Optimus.Graphics

Provides rendering for the Knyaz.Optimus

Knyaz.Optimus.Scripting.Jurassic

Plugin for the Knyaz.Optimus to connect Jurassic JavaScript engine.

Knyaz.Optimus.Scripting.Jint

Allows to use Jint to execute JavaScript in Optimus.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.1 5,618 10/31/2021
3.0.0 1,186 10/8/2021
3.0.0-beta-320 737 9/11/2021
3.0.0-beta-305 925 4/4/2021
3.0.0-beta-267 741 1/8/2021
3.0.0-beta-266 767 1/7/2021
2.4.1 2,281 11/21/2020
2.4.0 1,890 4/19/2020
2.3.6 1,209 3/15/2020
2.3.5 1,607 2/1/2020
2.3.4 1,496 1/11/2020
2.3.3 1,114 12/7/2019
2.3.2 1,044 11/24/2019
2.2.6 1,374 10/19/2019
2.2.5 1,507 7/24/2019
2.2.4 1,157 7/21/2019
2.2.3 6,597 5/8/2019
2.2.2 6,318 8/20/2018
2.1.43 2,485 3/12/2018
2.1.41 1,728 2/16/2018
2.0.39 1,600 12/22/2017
1.2.26 1,763 6/5/2017
1.2.23 1,477 5/3/2017
1.2.21 1,471 3/3/2017
1.2.20 1,719 12/1/2016
1.2.17 1,773 11/3/2016
1.2.15 1,481 10/3/2016
1.2.12 1,433 9/9/2016
1.2.9 1,369 8/29/2016
1.2.7 1,422 8/18/2016
1.2.4 1,423 8/16/2016
1.2.0 1,622 8/3/2016
1.1.0 1,536 8/3/2016
1.0.0 2,496 8/3/2016

Added support of UserAgent, Proxy and BasicAuth. Fixed bugs.