2

Is there a simple way to inject a javascript into a HtmlDocument object without using PhantomJS? Is there such a method in HtmlAgilityPack.HtmlDocument class?

Any advice is apreciated, Thank you!

2 Answers 2

1

You can inject a script tag (with script in it) in a HTMLDocument.

You still need something to execute the javascript though. If you want your c# code to somehow evaluate the result of the javascript - especially if you are doing dom manipulation - then PhantomJS is probably the best way to go as it's a headless browser that you can run without user interaction and interface.

The short answer is yes, but i think what you want is not possible without an engine that knows both javascript and the html dom. There are alternatives to PhantomJS like http://slimerjs.org/ so my cheeky answer is still yes ;)

Hint: Don't be afraid to run PhantomJS or the like.

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

3 Comments

Yes I am doing DOM manipulation. I think I will try to use PhantomJS after all. Can you please provide an example of how I can get a javascript variable in C#?
For example I have a variable var price = productPrice(); I want to evaluate the function productPrice() and get the price in C# using PhantomJS.
Are you doing testing on a website, or are you trying to automate a task, or are you perhaps 'scraping' a site? I think context will help a lot here.
1

You can create the element with

HtmlElement script = doc.CreateElement("SCRIPT");

Set the value with the script that you want and then attach to the Header of the HtmlDocument

Comments

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.