4

I would like to run javascript on HTML generated from C# string. (without opening browser - just C# code)

Something like:

string myHTML = '<div id="divTest">This is test<br/><br/></div>';

Javascript should be:

function test 
{
     return document.getElementById('divTest').offsetHeight;
}

What I've already tried is check some headless browsers like PhantomJS and Optimus - but they don't have an option to render string (only to open web page through web address like http://google.com/. Is there a way to achieve this through C#?

7
  • 1
    Are you appending string myHTML to your page HTML. This should be accessible from any browser. Commented Aug 30, 2017 at 15:12
  • if the html is placed in dom as html, it should be accessible Commented Aug 30, 2017 at 15:15
  • 1
    This is not entirely clear. You want to run javascript on a string of HTML, and get the height of the element.... outside a browser, i.e. in something like a command-line tool etc? Commented Aug 30, 2017 at 15:17
  • I've edited question - without opening browser, of course. Commented Aug 30, 2017 at 15:21
  • but they don't have an option to render string - what do you mean by this? Commented Aug 30, 2017 at 15:25

1 Answer 1

1

The following is working for me:

string html = "<html><head></head><body><div id=\"divTest\">This is test<br/><br/></div></body></html>";

string script1 = "return document.getElementById('divTest').innerText";
string script2 = "return document.getElementById('divTest').offsetHeight";

var options = new PhantomJSOptions();
options.AddAdditionalCapability("javascriptEnabled", true);

var driver = new PhantomJSDriver(options);
driver.Url = "file://dummy.html";
driver.Navigate();
driver.ExecutePhantomJS("document.write('" + html + "');");

var test1 = driver.ExecutePhantomJS(script1);
//Returns: This is a test
var test2 = driver.ExecutePhantomJS(script2);
//Returns: 40

You need to install the Selenium.WebDriver NuGet and to download the PhantomJS exe from http://phantomjs.org/download.html it needs to be in the same folder as your exe or in the PATH.

Sign up to request clarification or add additional context in comments.

5 Comments

I would like to run javascript without opening browser. Just C# code.
Do you have any example on this? Or link?
I added an example, haven't tested it myself but should work or at least get you in the right direction ;-)
I managed to make it work with a few tweaks, I edited the answer.
Your solution is good. The only thing which I don't know is how to set global variable for driver in MVC? I want to open PhantomJSDriver and keep it open (because this is much faster than open - close each time). It's not a problem to do this in windows application, but what about MVC?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.