Reputation: 141
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 path
to the phantomJS script as system.args[1]
and system.args[2]
. Any idea how to do this?
Upvotes: 0
Views: 2016
Reputation: 1
use arguments[0], arguments[1], ... to reference args. http://javadox.com/com.github.detro.ghostdriver/phantomjsdriver/1.1.0/org/openqa/selenium/phantomjs/PhantomJSDriver.html
Upvotes: 0
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
Reputation: 11609
Why don't you just pass arguments to executePhantomJS method?
driver.executePhantomJS(content, url, path);
Upvotes: 1