如何执行下载网页(args1)并将结果保存到文件(args2)中的phantomJS脚本,如下所示:
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);这段代码可以正常工作,除非我尝试将调用url和path的selenium/ghostdriver 2参数作为system.args[1]和system.args[2]传递给phantomJS脚本。你知道怎么做吗?
发布于 2016-01-20 15:54:12
为什么不直接将参数传递给executePhantomJS方法呢?
driver.executePhantomJS(content, url, path);发布于 2016-01-20 17:31:01
为了解决这个问题,我所做的不是将两个参数作为参数传递(我们不是在命令行中),而是将文件作为字符串进行编辑,并将这两个变量的值替换为String.replace()。
发布于 2016-09-05 19:57:53
使用参数,arguments1,...引用参数。http://javadox.com/com.github.detro.ghostdriver/phantomjsdriver/1.1.0/org/openqa/selenium/phantomjs/PhantomJSDriver.html
https://stackoverflow.com/questions/34894209
复制相似问题