0

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?

amarincolas
  • 141
  • 14
  • This is a really strange way of executing a PhantomJS script. If you use the Java bindings for Selenium, then you really should be using its API. If you want to call a plain PhantomJS script from Java, then invoke the PhantomJS binary as a subprocess with the script. Please don't mix both, because you will almost certainly run into problems when this gets a little more complicated. – Artjom B. Jan 22 '16 at 23:33
  • Do you mean call the PhantomJS script like a process instead of using Ghostdriver? I do not quite understand what you are saying about "you really should be using its API". Am I not doing it already? – amarincolas Jan 25 '16 at 10:52
  • 1
    Yes, I mean that you should use `Runtime#exec` or similar instead of executing the same script through Ghostdriver when you have a script. If you are not limited to a script, then you should use the API like opening a page with the `driver` and getting it's page source. – Artjom B. Jan 25 '16 at 11:21

3 Answers3

1

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

driver.executePhantomJS(content, url, path);
AdamSkywalker
  • 11,408
  • 3
  • 38
  • 76
  • Using what you say give the following args in the script: `main.js,--ip=,--port=36863,--logFile=phantomjsdriver.log,--logColor=false,--logLevel=INFO`. There is not a url arg or a path arg. – amarincolas Jan 20 '16 at 09:20
0

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().

amarincolas
  • 141
  • 14
0

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

alien
  • 1