amarincolas
amarincolas

Reputation: 141

How to pass arguments to a PhantomJS script from Selenium/Ghostdriver

I what to execute a phantomJS script which download a webpage (args[1]) and save the result html into a file (args[2]) as follows:

var system = require('system');
var page = require('webpage').create();
var fs = require('fs');

// Set the url address and the path
var url = system.args[1];
var path = system.args[2];

page.open(url, function () {
   fs.write(path, page.content, 'w');
   phantom.exit();
});

I am using selenium/ghostdriver to execute the script as follows:

DesiredCapabilities cap = new DesiredCapabilities();
cap.setJavascriptEnabled(true);
cap.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,".../phantomjs");

String [] phantomJsArgs = {url,path};
cap.setCapability(PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS, phantomJsArgs);

PhantomJSDriver driver = new PhantomJSDriver(cap);          
String content = new String(Files.readAllBytes(Paths.get(scriptPath)),Charset.forName("UTF-8"));
driver.executePhantomJS(content);

This code works except when I try to pass from selenium/ghostdriver 2 parameters call url and pathto the phantomJS script as system.args[1] and system.args[2]. Any idea how to do this?

Upvotes: 0

Views: 2016

Answers (3)

amarincolas
amarincolas

Reputation: 141

What I did to resolve the problem was instead of passing the 2 parameters as arguments (we are not in a command line), what I did was to edit the file as a string and replace the values of those two variables with String.replace().

Upvotes: 0

AdamSkywalker
AdamSkywalker

Reputation: 11609

Why don't you just pass arguments to executePhantomJS method?

driver.executePhantomJS(content, url, path);

Upvotes: 1

Related Questions