0

I've used PhantomJS in an C# application and it's not executing JavaScript even though the property PhantomJSDriver.Capabilities.IsJavaScriptEnabled is true. The simple page below still executes the content of the noscript tag. How can I make PhantomJS execute JavaScript?

I've added Selenium and PhantomJS to my VS2012 solution via NuGet:

PM> Install-Package Selenium.WebDriver 
PM> Install-Package PhantomJS 

I've created a simple HTML page to demonstrate that JavaScript is not enabled:

<html>
  <body>
    <a href="javascript:GoToAnotherPage()">Go to another page</a>
    <noscript>
      No JavaScript!
    </noscript>
  </body>
</html>

I've used the PhantomJSDriver. src displays "No Javascript!"

public class Program
{
    public static void Main(string[] args)
    {
        var phantomDriver = new PhantomJSDriver();
        phantomDriver.Url = @"C:\page.html";
        var src = phantomDriver.PageSource;
    }
}
3
  • 1
    I don't know how to get what you actually want, but PageSource is defined as "Gets the source of the page last loaded by the browser." So of course it includes everything in your HTML file. Commented Nov 19, 2014 at 14:44
  • Edited the question. There is a link in the body section which sends you to another page upon clicking. The page source does not change after. I thought that this was an JS issue.. I'new to Phantom. Commented Nov 19, 2014 at 14:55
  • I've used phantomDriver.GetScreenshot(); and it's blank. So it's not a JS problem after all.. Thanks. If you write it in an answer, i will accept it. Commented Nov 19, 2014 at 15:03

2 Answers 2

3

JavaScript is by default enabled when using PhantomJS. In fact I'm not aware that any WebDriver starts their browser without JavaScript by default.

To make sure that JavaScript is enabled, you can check

var phantomDriver = new PhantomJSDriver();
var enabled = phantomDriver.Capabilities.IsJavaScriptEnabled;

You can also check experimentally that the JavaScript is running by taking a screenshot and checking that the noscript block is actually not shown. So when the screenshot (phantomDriver.GetScreenshot();) is blank in your case then it works.

It is by the way a bad idea to disable JavaScript for the PhantomJSDriver, because many operations of the WebDriver protocol are implemented in JavaScript. Disabling JS would effectively disable the driver.

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

1 Comment

Hi, thanks for the answer. Even though you weren't first to offer the solution I've accepted yours because the other was only in comment.
2

PageSource is not supposed to execute JavaScript, it gets the source of the page last loaded by the browser, so it includes everything in your HTML file.

To see the actual state of the page use

phantomDriver.GetScreenshot();

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.