Reputation: 1442
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;
}
}
Upvotes: 0
Views: 4268
Reputation: 61892
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.
Upvotes: 3
Reputation: 1442
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();
Upvotes: 2