如何将参数从Selenium/Ghostdriver传递给PhantomJS脚本



我执行一个phantomJS脚本,下载一个网页(args[1])并将结果html保存到一个文件(args[2])中,如下所示:

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();
});

我正在使用selenium/ghostdriver来执行脚本,如下所示:

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);

除非我尝试selenium/ghostdriver从 2 个参数调用 urlpath 作为system.args[1]system.args[2]传递给 phantomJS 脚本,否则此代码有效。知道怎么做吗?

你为什么不直接传递参数来执行PhantomJS方法?

driver.executePhantomJS(content, url, path);

我为解决问题所做的不是将 2 个参数作为参数传递(我们不在命令行中),我所做的是将文件编辑为字符串并将这两个变量的值替换为 String.replace() .

使用参数[0], 参数[1], ...以引用参数。http://javadox.com/com.github.detro.ghostdriver/phantomjsdriver/1.1.0/org/openqa/selenium/phantomjs/PhantomJSDriver.html

最新更新