Optimus headless browser with C#

1.5k views Asked by At

Can somebody tell me how to use Optimus (headless browser) nuget package with C# to get response from a URL. I also want javascript on the page to be executed automatically like phantomjs.

1

There are 1 answers

3
SagarScript On

Quite a simple bit of kit:

  1. Create an Engine component first (common for dynamic and static pages):

    Engine engine = new Engine();

  2. Open the url of the html document you want to retreive:

    a) Not waiting for any elements added in with javascript:

    engine.OpenUrl("http://google.com").Wait();

    b) Waiting for any elements added in with javascript:

    engine.OpenUrl("http://google.com")

    and then either:

    • engine.WaitDesappearingOfId("some-id")
    • engine.WaitId("some-id")
    • engine.WaitDocumentLoad()
    • engine.WaitSelector("#some-id")
    • engine.WaitSelector(".some-class")

now you open the url, there are two ways of doing this - load the document (prior to any javascript being executed):

More complete examples:

public static string dynamicLoadingPage()
{
    var engine = new Engine();
    engine.OpenUrl("https://html5test.com");
    var tagWithValue = engine.WaitSelector("#score strong").FirstOrDefault();
    System.Console.WriteLine("Score: " + tagWithValue.InnerHTML);
}

Otherwise:

static string staticLoadingPage()
{
   var engine = new Engine();
   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);

}